Add HTTP server: event store, optional login, static files
Standard library only. Config from env vars, validated at startup. State is an append-only events.jsonl (idempotent appends, torn-line tolerant) plus notes and settings written atomically. Optional AUTH_PASSWORD login with signed HttpOnly SameSite=Strict cookies and per-IP backoff. CSRF: JSON-only writes plus an Origin check. Strict CSP, ETag + gzip, BASE_PATH support. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gXkMmmRRKLSnY6vQN3
This commit is contained in:
parent
226e5f46f8
commit
67f9b199ec
7 changed files with 1033 additions and 0 deletions
91
server/config.js
Normal file
91
server/config.js
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
// Runtime configuration, read once from environment variables.
|
||||
// Every variable is documented in .env.example and README.md; keep the three in sync.
|
||||
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
|
||||
function str(env, name, fallback) {
|
||||
const v = env[name];
|
||||
return v === undefined || v.trim() === '' ? fallback : v.trim();
|
||||
}
|
||||
|
||||
function int(env, name, fallback, min, max) {
|
||||
const raw = str(env, name, null);
|
||||
if (raw === null) return fallback;
|
||||
const n = Number(raw);
|
||||
if (!Number.isInteger(n) || n < min || n > max) {
|
||||
throw new Error(`${name} must be an integer between ${min} and ${max} (got "${raw}")`);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function num(env, name, fallback, min, max) {
|
||||
const raw = str(env, name, null);
|
||||
if (raw === null) return fallback;
|
||||
const n = Number(raw);
|
||||
if (!Number.isFinite(n) || n < min || n > max) {
|
||||
throw new Error(`${name} must be a number between ${min} and ${max} (got "${raw}")`);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function bool(env, name, fallback) {
|
||||
const raw = str(env, name, null);
|
||||
if (raw === null) return fallback;
|
||||
if (/^(1|true|yes|on)$/i.test(raw)) return true;
|
||||
if (/^(0|false|no|off)$/i.test(raw)) return false;
|
||||
throw new Error(`${name} must be true or false (got "${raw}")`);
|
||||
}
|
||||
|
||||
function oneOf(env, name, fallback, allowed) {
|
||||
const raw = str(env, name, fallback).toLowerCase();
|
||||
if (!allowed.includes(raw)) throw new Error(`${name} must be one of ${allowed.join(', ')} (got "${raw}")`);
|
||||
return raw;
|
||||
}
|
||||
|
||||
/** Normalise BASE_PATH to "/", or "/sub/path/" (leading and trailing slash). */
|
||||
function basePath(env) {
|
||||
const raw = str(env, 'BASE_PATH', '/');
|
||||
if (!/^[A-Za-z0-9/_.-]*$/.test(raw) || raw.includes('..')) {
|
||||
throw new Error(`BASE_PATH may only contain letters, digits, "/", "_", "-", "." (got "${raw}")`);
|
||||
}
|
||||
const trimmed = raw.replace(/^\/+|\/+$/g, '');
|
||||
return trimmed ? `/${trimmed}/` : '/';
|
||||
}
|
||||
|
||||
export function loadConfig(env = process.env) {
|
||||
const authPassword = env.AUTH_PASSWORD ?? '';
|
||||
const config = {
|
||||
host: str(env, 'HOST', '127.0.0.1'),
|
||||
port: int(env, 'PORT', 8317, 1, 65535),
|
||||
basePath: basePath(env),
|
||||
dataDir: path.resolve(ROOT, str(env, 'DATA_DIR', 'var')),
|
||||
publicDir: path.join(ROOT, 'public'),
|
||||
authPassword: authPassword.length ? authPassword : null,
|
||||
sessionSecret: str(env, 'SESSION_SECRET', null),
|
||||
sessionDays: int(env, 'SESSION_DAYS', 180, 1, 3650),
|
||||
cookieSecure: oneOf(env, 'COOKIE_SECURE', 'auto', ['auto', 'true', 'false']),
|
||||
trustProxy: bool(env, 'TRUST_PROXY', false),
|
||||
logRequests: bool(env, 'LOG_REQUESTS', false),
|
||||
app: {
|
||||
title: str(env, 'APP_TITLE', 'Elorasmus').slice(0, 60),
|
||||
stayMonths: num(env, 'STAY_MONTHS', 5, 1, 12),
|
||||
eloInitial: int(env, 'ELO_INITIAL', 1500, 100, 5000),
|
||||
eloKMax: int(env, 'ELO_K_MAX', 48, 4, 200),
|
||||
eloKMin: int(env, 'ELO_K_MIN', 16, 1, 200),
|
||||
eurCzk: num(env, 'EUR_CZK', null, 1, 100),
|
||||
},
|
||||
};
|
||||
if (config.authPassword && config.authPassword.length < 8) {
|
||||
throw new Error('AUTH_PASSWORD must be at least 8 characters (or unset to disable login)');
|
||||
}
|
||||
if (config.sessionSecret && config.sessionSecret.length < 32) {
|
||||
throw new Error('SESSION_SECRET must be at least 32 characters (or unset to auto-generate one)');
|
||||
}
|
||||
if (config.app.eloKMin > config.app.eloKMax) {
|
||||
throw new Error('ELO_K_MIN must not be larger than ELO_K_MAX');
|
||||
}
|
||||
return config;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue