Use JSON server configuration and repository database name
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { SERVER_LOG_LEVELS } from '../../constants/server.js';
|
||||
import { parseTrustProxy } from './http-operations.js';
|
||||
|
||||
export interface ServerConfig {
|
||||
MONGODB_URI: string; MONGODB_DATABASE: string; HOST: string; PORT: number;
|
||||
TRUST_PROXY: string; LOG_LEVEL: string; ADMIN_USER: string; ADMIN_PASSWORD: string;
|
||||
STUN_URLS: string[]; TURN_URLS: string[]; TURN_SHARED_SECRET: string;
|
||||
}
|
||||
const invalid = () => Object.assign(new Error('INVALID_CONFIG'), { code: 'INVALID_CONFIG' });
|
||||
export function loadServerConfig(path = resolve('.config.json')): ServerConfig {
|
||||
let input: unknown;
|
||||
try { input = JSON.parse(readFileSync(path, 'utf8').replace(/^\uFEFF/, '')); }
|
||||
catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') throw Object.assign(new Error('CONFIG_NOT_FOUND'), { code: 'CONFIG_NOT_FOUND' });
|
||||
throw invalid();
|
||||
}
|
||||
if (!input || typeof input !== 'object' || Array.isArray(input)) throw invalid();
|
||||
const value = input as Record<string, unknown>;
|
||||
const text = (key: string, fallback?: string): string => {
|
||||
const result = value[key] ?? fallback;
|
||||
if (typeof result !== 'string' || (fallback === undefined && !result.trim())) throw invalid();
|
||||
return result;
|
||||
};
|
||||
const urls = (key: string): string[] => {
|
||||
const result = value[key] ?? [];
|
||||
if (!Array.isArray(result) || result.some(url => typeof url !== 'string' || !url.trim())) throw invalid();
|
||||
return result.map(url => url.trim());
|
||||
};
|
||||
const config: ServerConfig = {
|
||||
MONGODB_URI: text('MONGODB_URI'), MONGODB_DATABASE: text('MONGODB_DATABASE'),
|
||||
HOST: text('HOST', '127.0.0.1'), PORT: (value.PORT ?? 2100) as number,
|
||||
TRUST_PROXY: text('TRUST_PROXY', ''), LOG_LEVEL: text('LOG_LEVEL', 'info'),
|
||||
ADMIN_USER: text('ADMIN_USER', ''), ADMIN_PASSWORD: text('ADMIN_PASSWORD', ''),
|
||||
STUN_URLS: urls('STUN_URLS'), TURN_URLS: urls('TURN_URLS'), TURN_SHARED_SECRET: text('TURN_SHARED_SECRET', ''),
|
||||
};
|
||||
if (!/^mongodb(?:\+srv)?:\/\//.test(config.MONGODB_URI) || !config.HOST.trim()
|
||||
|| !Number.isInteger(config.PORT) || config.PORT < 1 || config.PORT > 65535
|
||||
|| !SERVER_LOG_LEVELS.some(level => level === config.LOG_LEVEL)) throw invalid();
|
||||
try { parseTrustProxy(config.TRUST_PROXY); } catch { throw invalid(); }
|
||||
return config;
|
||||
}
|
||||
+4
-5
@@ -1,15 +1,14 @@
|
||||
import { resolve } from 'node:path';
|
||||
import { createGameServer } from './create-server.js';
|
||||
import { logSafeError, parseTrustProxy } from './http-operations.js';
|
||||
const { MONGODB_URI, MONGODB_DATABASE = 'dragon_ball', HOST = '127.0.0.1', PORT = '2100', ADMIN_USER, ADMIN_PASSWORD } = process.env;
|
||||
const urls = (value: string | undefined) => value?.split(',').map(s => s.trim()).filter(Boolean);
|
||||
import { loadServerConfig } from './config.js';
|
||||
const startupLogger = { error: (data: unknown) => console.error(JSON.stringify({ level: 50, time: Date.now(), ...data as object })) };
|
||||
let app: Awaited<ReturnType<typeof createGameServer>> | undefined;
|
||||
try {
|
||||
if (!MONGODB_URI) throw Object.assign(new Error(), { code: 'MISSING_MONGODB_URI' });
|
||||
const { MONGODB_URI, MONGODB_DATABASE, HOST, PORT, ADMIN_USER, ADMIN_PASSWORD, TRUST_PROXY, LOG_LEVEL, STUN_URLS, TURN_URLS, TURN_SHARED_SECRET } = loadServerConfig();
|
||||
app = await createGameServer({ mongoUri: MONGODB_URI, databaseName: MONGODB_DATABASE, clientDirectory: resolve('dist/client'), adminUser: ADMIN_USER, adminPassword: ADMIN_PASSWORD,
|
||||
trustProxy: parseTrustProxy(process.env.TRUST_PROXY), logLevel: process.env.LOG_LEVEL,
|
||||
rtc: { stunUrls: urls(process.env.STUN_URLS), turnUrls: urls(process.env.TURN_URLS), turnSecret: process.env.TURN_SHARED_SECRET } });
|
||||
trustProxy: parseTrustProxy(TRUST_PROXY), logLevel: LOG_LEVEL,
|
||||
rtc: { stunUrls: STUN_URLS, turnUrls: TURN_URLS, turnSecret: TURN_SHARED_SECRET } });
|
||||
let closing = false;
|
||||
for (const signal of ['SIGINT', 'SIGTERM'] as const) process.once(signal, () => {
|
||||
if (closing) return;
|
||||
|
||||
Reference in New Issue
Block a user