mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-18 08:18:43 +02:00
add extra validation to environment.ts and use os paths
This commit is contained in:
parent
497b8a223f
commit
9c40d8a5c7
5 changed files with 41 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
||||||
import { defineConfig } from "drizzle-kit";
|
import { defineConfig } from "drizzle-kit";
|
||||||
import environment from "@server/environment"
|
import environment from "@server/environment";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
dialect: "sqlite",
|
dialect: "sqlite",
|
||||||
|
@ -7,6 +8,6 @@ export default defineConfig({
|
||||||
out: "server/migrations",
|
out: "server/migrations",
|
||||||
verbose: true,
|
verbose: true,
|
||||||
dbCredentials: {
|
dbCredentials: {
|
||||||
url: `${environment.CONFIG_PATH}/db/db.sqlite`
|
url: path.join(environment.CONFIG_PATH, "db", "db.sqlite"),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,8 +2,11 @@ import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||||
import Database from "better-sqlite3";
|
import Database from "better-sqlite3";
|
||||||
import * as schema from "@server/db/schema";
|
import * as schema from "@server/db/schema";
|
||||||
import environment from "@server/environment";
|
import environment from "@server/environment";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
const sqlite = new Database(`${environment.CONFIG_PATH}/db/db.sqlite`);
|
const sqlite = new Database(
|
||||||
|
path.join(environment.CONFIG_PATH, "db", "db.sqlite"),
|
||||||
|
);
|
||||||
export const db = drizzle(sqlite, { schema });
|
export const db = drizzle(sqlite, { schema });
|
||||||
|
|
||||||
export default db;
|
export default db;
|
||||||
|
|
|
@ -1,13 +1,26 @@
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
const environmentSchema = z.object({
|
const environmentSchema = z.object({
|
||||||
ENVIRONMENT: z.string(),
|
ENVIRONMENT: z.enum(["dev", "prod"]),
|
||||||
LOG_LEVEL: z.string(),
|
LOG_LEVEL: z.enum(["debug", "info", "warn", "error"]),
|
||||||
SAVE_LOGS: z.string().transform((val) => val === "true"),
|
SAVE_LOGS: z.string().transform((val) => val === "true"),
|
||||||
PORT: z.string(),
|
EXTERNAL_PORT: z
|
||||||
INTERNAL_PORT: z.string(),
|
.string()
|
||||||
CONFIG_PATH: z.string(),
|
.transform((val) => parseInt(val, 10))
|
||||||
|
.pipe(z.number()),
|
||||||
|
INTERNAL_PORT: z
|
||||||
|
.string()
|
||||||
|
.transform((val) => parseInt(val, 10))
|
||||||
|
.pipe(z.number()),
|
||||||
|
CONFIG_PATH: z.string().transform((val) => {
|
||||||
|
// validate the path and remove any trailing slashes
|
||||||
|
const resolvedPath = path.resolve(val);
|
||||||
|
return resolvedPath.endsWith(path.sep)
|
||||||
|
? resolvedPath.slice(0, -1)
|
||||||
|
: resolvedPath;
|
||||||
|
}),
|
||||||
API_VERSION: z.string(),
|
API_VERSION: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -15,9 +28,10 @@ const environment = {
|
||||||
ENVIRONMENT: (process.env.ENVIRONMENT as string) || "dev",
|
ENVIRONMENT: (process.env.ENVIRONMENT as string) || "dev",
|
||||||
LOG_LEVEL: (process.env.LOG_LEVEL as string) || "debug",
|
LOG_LEVEL: (process.env.LOG_LEVEL as string) || "debug",
|
||||||
SAVE_LOGS: (process.env.SAVE_LOGS as string) || "false",
|
SAVE_LOGS: (process.env.SAVE_LOGS as string) || "false",
|
||||||
PORT: (process.env.PORT as string) || "3000",
|
EXTERNAL_PORT: (process.env.EXTERNAL_PORT as string) || "3000",
|
||||||
INTERNAL_PORT: (process.env.INTERNAL_PORT as string) || "3001",
|
INTERNAL_PORT: (process.env.INTERNAL_PORT as string) || "3001",
|
||||||
CONFIG_PATH: process.env.CONFIG_PATH as string,
|
CONFIG_PATH:
|
||||||
|
(process.env.CONFIG_PATH as string) || path.join(__dirname, "config"),
|
||||||
API_VERSION: (process.env.API_VERSION as string) || "v1",
|
API_VERSION: (process.env.API_VERSION as string) || "v1",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import external from "@server/routers/external";
|
||||||
const dev = environment.ENVIRONMENT !== "prod";
|
const dev = environment.ENVIRONMENT !== "prod";
|
||||||
const app = next({ dev });
|
const app = next({ dev });
|
||||||
const handle = app.getRequestHandler();
|
const handle = app.getRequestHandler();
|
||||||
const mainPort = environment.PORT;
|
const mainPort = environment.EXTERNAL_PORT;
|
||||||
const internalPort = environment.INTERNAL_PORT;
|
const internalPort = environment.INTERNAL_PORT;
|
||||||
|
|
||||||
app.prepare().then(() => {
|
app.prepare().then(() => {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import "winston-daily-rotate-file";
|
import "winston-daily-rotate-file";
|
||||||
|
|
||||||
import environment from "@server/environment";
|
import environment from "@server/environment";
|
||||||
import * as winston from "winston";
|
import * as winston from "winston";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
const hformat = winston.format.printf(
|
const hformat = winston.format.printf(
|
||||||
({ level, label, message, timestamp, ...metadata }) => {
|
({ level, label, message, timestamp, ...metadata }) => {
|
||||||
|
@ -27,7 +27,11 @@ const transports: any = [
|
||||||
if (environment.SAVE_LOGS) {
|
if (environment.SAVE_LOGS) {
|
||||||
transports.push(
|
transports.push(
|
||||||
new winston.transports.DailyRotateFile({
|
new winston.transports.DailyRotateFile({
|
||||||
filename: `${environment.CONFIG_PATH}/logs/pangolin-%DATE%.log`,
|
filename: path.join(
|
||||||
|
environment.CONFIG_PATH,
|
||||||
|
"logs",
|
||||||
|
"pangolin-%DATE%.log",
|
||||||
|
),
|
||||||
datePattern: "YYYY-MM-DD",
|
datePattern: "YYYY-MM-DD",
|
||||||
zippedArchive: true,
|
zippedArchive: true,
|
||||||
maxSize: "20m",
|
maxSize: "20m",
|
||||||
|
@ -38,7 +42,11 @@ if (environment.SAVE_LOGS) {
|
||||||
);
|
);
|
||||||
transports.push(
|
transports.push(
|
||||||
new winston.transports.DailyRotateFile({
|
new winston.transports.DailyRotateFile({
|
||||||
filename: `${environment.CONFIG_PATH}/logs/.machinelogs-%DATE%.json`,
|
filename: path.join(
|
||||||
|
environment.CONFIG_PATH,
|
||||||
|
"logs",
|
||||||
|
".machinelogs-%DATE%.json",
|
||||||
|
),
|
||||||
datePattern: "YYYY-MM-DD",
|
datePattern: "YYYY-MM-DD",
|
||||||
zippedArchive: true,
|
zippedArchive: true,
|
||||||
maxSize: "20m",
|
maxSize: "20m",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue