zerobyte/app/server/core/config.ts
James Brink eded452c83
feat: parameterize PORT and MIGRATIONS_PATH environment variables (#253)
* feat: parameterize PORT and MIGRATIONS_PATH environment variables

Allow configuring the server port (default: 4096) and migrations
folder path via environment variables for flexible deployment.

* fix: address PR review feedback

- Use consistent schema syntax (string? instead of key?)
- Remove config tests per maintainer request
2025-12-29 20:59:38 +01:00

32 lines
928 B
TypeScript

import { type } from "arktype";
import "dotenv/config";
const envSchema = type({
NODE_ENV: type.enumerated("development", "production", "test").default("production"),
SERVER_IP: 'string = "localhost"',
SERVER_IDLE_TIMEOUT: 'string.integer.parse = "60"',
RESTIC_HOSTNAME: "string = 'zerobyte'",
PORT: 'string.integer.parse = "4096"',
MIGRATIONS_PATH: "string?",
}).pipe((s) => ({
__prod__: s.NODE_ENV === "production",
environment: s.NODE_ENV,
serverIp: s.SERVER_IP,
serverIdleTimeout: s.SERVER_IDLE_TIMEOUT,
resticHostname: s.RESTIC_HOSTNAME,
port: s.PORT,
migrationsPath: s.MIGRATIONS_PATH,
}));
const parseConfig = (env: unknown) => {
const result = envSchema(env);
if (result instanceof type.errors) {
console.error(`Environment variable validation failed: ${result.toString()}`);
throw new Error("Invalid environment variables");
}
return result;
};
export const config = parseConfig(process.env);