Files
ignis/apps/ignis-server/server/settings.js

115 lines
2.5 KiB
JavaScript
Raw Normal View History

2026-06-06 01:16:01 +02:00
const fs = require("fs");
const path = require("path");
const config = require("./config");
// Runtime server settings set through UI.
const SETTINGS_FILE = path.join(config.dataRoot, "server-settings.json");
const DEFAULTS = {
contentCacheBytes: 50 * 1024 * 1024,
inputCacheBytes: 200 * 1024 * 1024,
inputCacheTtlMs: 5 * 60 * 1000,
2026-06-06 20:40:42 +02:00
writeCoalesceMs: 0,
2026-06-06 01:16:01 +02:00
maxBodyBytes: 50 * 1024 * 1024,
2026-06-06 17:05:26 +02:00
// "any" reaches any public host, "allowlist" restricts to proxyAllowlist, "disabled" blocks all proxying.
proxyMode: "any",
// Empty allows any public host.
2026-06-06 01:16:01 +02:00
proxyAllowlist: [],
wsOrigins: [],
};
2026-06-06 17:05:26 +02:00
const PROXY_MODES = ["any", "allowlist", "disabled"];
2026-06-06 01:16:01 +02:00
const KEYS = Object.keys(DEFAULTS);
2026-06-06 17:05:26 +02:00
// Env vars only; never persisted to the settings file.
const ENV_ONLY_KEYS = ["wsOrigins"];
2026-06-06 13:04:34 +02:00
// Hard ceiling for request bodies.
const MAX_BODY_BACKSTOP = 500 * 1024 * 1024;
2026-06-06 01:16:01 +02:00
function parseList(raw) {
return raw
.split(",")
.map((s) => s.trim())
.filter(Boolean);
}
function fromEnv() {
const env = {};
if (process.env.WRITE_COALESCE_MS !== undefined) {
const n = parseInt(process.env.WRITE_COALESCE_MS, 10);
if (Number.isFinite(n)) {
env.writeCoalesceMs = n;
}
}
if (process.env.WS_ORIGINS) {
env.wsOrigins = parseList(process.env.WS_ORIGINS);
}
return env;
}
const envOverrides = fromEnv();
function loadFile() {
try {
const parsed = JSON.parse(fs.readFileSync(SETTINGS_FILE, "utf-8"));
// Keep only known keys so a stale or hand-edited file can't inject junk.
const clean = {};
for (const key of KEYS) {
2026-06-06 17:05:26 +02:00
if (ENV_ONLY_KEYS.includes(key)) {
continue;
}
2026-06-06 01:16:01 +02:00
if (parsed[key] !== undefined) {
clean[key] = parsed[key];
}
}
return clean;
} catch {
return {};
}
}
let fileOverrides = loadFile();
function getAll() {
return { ...DEFAULTS, ...envOverrides, ...fileOverrides };
}
function get(key) {
return getAll()[key];
}
// Merge validated changes into the persisted file and return the new effective settings.
function update(partial) {
for (const [key, value] of Object.entries(partial)) {
2026-06-06 17:05:26 +02:00
if (KEYS.includes(key) && !ENV_ONLY_KEYS.includes(key)) {
2026-06-06 01:16:01 +02:00
fileOverrides[key] = value;
}
}
fs.mkdirSync(path.dirname(SETTINGS_FILE), { recursive: true });
fs.writeFileSync(SETTINGS_FILE, JSON.stringify(fileOverrides, null, 2));
return getAll();
}
2026-06-06 17:05:26 +02:00
module.exports = {
DEFAULTS,
KEYS,
ENV_ONLY_KEYS,
PROXY_MODES,
MAX_BODY_BACKSTOP,
getAll,
get,
update,
};