mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-28 05:44:01 +02:00
set public next env vars from config
This commit is contained in:
parent
cf1de2253b
commit
61fca6a1f6
7 changed files with 69 additions and 33 deletions
|
@ -4,11 +4,12 @@ app:
|
||||||
base_url: http://localhost:3000
|
base_url: http://localhost:3000
|
||||||
log_level: debug
|
log_level: debug
|
||||||
save_logs: "false"
|
save_logs: "false"
|
||||||
secure_cookies: "false"
|
|
||||||
|
|
||||||
server:
|
server:
|
||||||
external_port: "3000"
|
external_port: "3000"
|
||||||
internal_port: "3001"
|
internal_port: "3001"
|
||||||
|
internal_hostname: localhost
|
||||||
|
secure_cookies: "false"
|
||||||
|
|
||||||
rate_limit:
|
rate_limit:
|
||||||
window_minutes: "1"
|
window_minutes: "1"
|
||||||
|
|
|
@ -24,13 +24,9 @@ export const lucia = new Lucia(adapter, {
|
||||||
expires: false,
|
expires: false,
|
||||||
attributes: {
|
attributes: {
|
||||||
sameSite: "strict",
|
sameSite: "strict",
|
||||||
secure: config.app.secure_cookies || false,
|
secure: config.server.secure_cookies || false,
|
||||||
domain:
|
domain:
|
||||||
"." +
|
"." + new URL(config.app.base_url).hostname.split(".").slice(-2).join("."),
|
||||||
config.app.external_base_url
|
|
||||||
.split("://")[1]
|
|
||||||
.split(":")[0]
|
|
||||||
.split("/")[0],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
sessionExpiresIn: new TimeSpan(2, "w"),
|
sessionExpiresIn: new TimeSpan(2, "w"),
|
||||||
|
|
|
@ -10,11 +10,9 @@ const environmentSchema = z.object({
|
||||||
app: z.object({
|
app: z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
environment: z.enum(["dev", "prod"]),
|
environment: z.enum(["dev", "prod"]),
|
||||||
external_base_url: z.string().url(),
|
base_url: z.string().url(),
|
||||||
internal_base_url: z.string().url(),
|
|
||||||
log_level: z.enum(["debug", "info", "warn", "error"]),
|
log_level: z.enum(["debug", "info", "warn", "error"]),
|
||||||
save_logs: z.string().transform((val) => val === "true"),
|
save_logs: z.string().transform((val) => val === "true"),
|
||||||
secure_cookies: z.string().transform((val) => val === "true"),
|
|
||||||
}),
|
}),
|
||||||
server: z.object({
|
server: z.object({
|
||||||
external_port: z
|
external_port: z
|
||||||
|
@ -25,6 +23,8 @@ const environmentSchema = z.object({
|
||||||
.string()
|
.string()
|
||||||
.transform((val) => parseInt(val, 10))
|
.transform((val) => parseInt(val, 10))
|
||||||
.pipe(z.number()),
|
.pipe(z.number()),
|
||||||
|
internal_hostname: z.string(),
|
||||||
|
secure_cookies: z.string().transform((val) => val === "true"),
|
||||||
}),
|
}),
|
||||||
rate_limit: z.object({
|
rate_limit: z.object({
|
||||||
window_minutes: z
|
window_minutes: z
|
||||||
|
@ -71,9 +71,19 @@ const loadConfig = (configPath: string) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const configFilePath = path.join(APP_PATH, "config.yml");
|
const configFilePath1 = path.join(APP_PATH, "config.yml");
|
||||||
|
const configFilePath2 = path.join(APP_PATH, "config.yaml");
|
||||||
|
|
||||||
const environment = loadConfig(configFilePath);
|
let environment: any;
|
||||||
|
if (fs.existsSync(configFilePath1)) {
|
||||||
|
environment = loadConfig(configFilePath1);
|
||||||
|
} else if (fs.existsSync(configFilePath2)) {
|
||||||
|
environment = loadConfig(configFilePath2);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!environment) {
|
||||||
|
throw new Error("No configuration file found");
|
||||||
|
}
|
||||||
|
|
||||||
const parsedConfig = environmentSchema.safeParse(environment);
|
const parsedConfig = environmentSchema.safeParse(environment);
|
||||||
|
|
||||||
|
@ -82,4 +92,13 @@ if (!parsedConfig.success) {
|
||||||
throw new Error(`Invalid configuration file: ${errors}`);
|
throw new Error(`Invalid configuration file: ${errors}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
process.env.NEXT_PUBLIC_EXTERNAL_API_BASE_URL = new URL(
|
||||||
|
"/api/v1",
|
||||||
|
parsedConfig.data.app.base_url,
|
||||||
|
).href;
|
||||||
|
process.env.NEXT_PUBLIC_INTERNAL_API_BASE_URL = new URL(
|
||||||
|
"/api/v1",
|
||||||
|
`http://${parsedConfig.data.server.internal_hostname}:${parsedConfig.data.server.external_port}`,
|
||||||
|
).href;
|
||||||
|
|
||||||
export default parsedConfig.data;
|
export default parsedConfig.data;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
import config from "@server/config";
|
||||||
import express, { Request, Response } from "express";
|
import express, { Request, Response } from "express";
|
||||||
import next from "next";
|
import next from "next";
|
||||||
import { parse } from "url";
|
import { parse } from "url";
|
||||||
import config from "@server/config";
|
|
||||||
import logger from "@server/logger";
|
import logger from "@server/logger";
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
|
|
|
@ -118,7 +118,7 @@ export async function login(
|
||||||
|
|
||||||
const session = await lucia.createSession(existingUser.id, {});
|
const session = await lucia.createSession(existingUser.id, {});
|
||||||
const cookie = lucia.createSessionCookie(session.id).serialize();
|
const cookie = lucia.createSessionCookie(session.id).serialize();
|
||||||
logger.debug("Session cookie", JSON.stringify(cookie, null, 2));
|
|
||||||
res.appendHeader(
|
res.appendHeader(
|
||||||
"Set-Cookie",
|
"Set-Cookie",
|
||||||
cookie
|
cookie
|
||||||
|
|
|
@ -37,7 +37,10 @@ export function buildTraefikConfig(
|
||||||
[middlewareName]: {
|
[middlewareName]: {
|
||||||
plugin: {
|
plugin: {
|
||||||
[middlewareName]: {
|
[middlewareName]: {
|
||||||
apiBaseUrl: config.app.internal_base_url,
|
apiBaseUrl: new URL(
|
||||||
|
"/api/v1",
|
||||||
|
`http://${config.server.internal_hostname}:${config.server.internal_port}`,
|
||||||
|
).href,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,24 +1,36 @@
|
||||||
import { Request, Response, NextFunction } from 'express';
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { z } from 'zod';
|
import { z } from "zod";
|
||||||
import { db } from '@server/db';
|
import { db } from "@server/db";
|
||||||
import { users } from '@server/db/schema';
|
import { users } from "@server/db/schema";
|
||||||
import { eq } from 'drizzle-orm';
|
import { eq } from "drizzle-orm";
|
||||||
import response from "@server/utils/response";
|
import response from "@server/utils/response";
|
||||||
import HttpCode from '@server/types/HttpCode';
|
import HttpCode from "@server/types/HttpCode";
|
||||||
import createHttpError from 'http-errors';
|
import createHttpError from "http-errors";
|
||||||
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
|
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
||||||
import logger from '@server/logger';
|
import logger from "@server/logger";
|
||||||
|
|
||||||
|
export type GetUserResponse = {
|
||||||
|
email: string;
|
||||||
|
twoFactorEnabled: boolean;
|
||||||
|
emailVerified: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
export async function getUser(req: Request, res: Response, next: NextFunction): Promise<any> {
|
export async function getUser(
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction,
|
||||||
|
): Promise<any> {
|
||||||
try {
|
try {
|
||||||
const userId = req.user?.id;
|
const userId = req.user?.id;
|
||||||
|
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
return next(createHttpError(HttpCode.UNAUTHORIZED, "User not found"));
|
return next(
|
||||||
|
createHttpError(HttpCode.UNAUTHORIZED, "User not found"),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await db.select()
|
const user = await db
|
||||||
|
.select()
|
||||||
.from(users)
|
.from(users)
|
||||||
.where(eq(users.id, userId))
|
.where(eq(users.id, userId))
|
||||||
.limit(1);
|
.limit(1);
|
||||||
|
@ -27,16 +39,16 @@ export async function getUser(req: Request, res: Response, next: NextFunction):
|
||||||
return next(
|
return next(
|
||||||
createHttpError(
|
createHttpError(
|
||||||
HttpCode.NOT_FOUND,
|
HttpCode.NOT_FOUND,
|
||||||
`User with ID ${userId} not found`
|
`User with ID ${userId} not found`,
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response(res, {
|
return response<GetUserResponse>(res, {
|
||||||
data: {
|
data: {
|
||||||
email: user[0].email,
|
email: user[0].email,
|
||||||
twoFactorEnabled: user[0].twoFactorEnabled,
|
twoFactorEnabled: user[0].twoFactorEnabled,
|
||||||
emailVerified: user[0].emailVerified
|
emailVerified: user[0].emailVerified,
|
||||||
},
|
},
|
||||||
success: true,
|
success: true,
|
||||||
error: false,
|
error: false,
|
||||||
|
@ -45,6 +57,11 @@ export async function getUser(req: Request, res: Response, next: NextFunction):
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
return next(
|
||||||
|
createHttpError(
|
||||||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||||||
|
"An error occurred...",
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue