- New server/config.mjs loads config.json, resolves secrets path relative to config dir, returns users/smtp/mail_perms_path/bind/port - server/secrets.mjs removed (logic absorbed into config.mjs) - smtp moves from secrets.json to config.json - secrets.json now contains only users (pure credentials) - config.example.json added as reference template - .gitignore/.npmignore updated to cover config.json and mail-perms.json - README updated with new setup and flags Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
import { readFileSync } from 'fs';
|
|
import { resolve } from 'path';
|
|
|
|
export function load_config(file_path) {
|
|
if (!file_path) {
|
|
throw new Error('--config <path> is required');
|
|
}
|
|
let raw;
|
|
try {
|
|
raw = readFileSync(file_path, 'utf8');
|
|
} catch (err) {
|
|
throw new Error(`Cannot read config file at ${file_path}: ${err.message}`);
|
|
}
|
|
let parsed;
|
|
try {
|
|
parsed = JSON.parse(raw);
|
|
} catch (err) {
|
|
throw new Error(`Config file is not valid JSON: ${err.message}`);
|
|
}
|
|
|
|
if (!parsed.secrets) {
|
|
throw new Error("Config file must have a 'secrets' field pointing to the secrets file");
|
|
}
|
|
|
|
// Resolve secrets path relative to the config file's directory
|
|
const config_dir = resolve(file_path, '..');
|
|
const secrets_path = resolve(config_dir, parsed.secrets);
|
|
|
|
let secrets_raw;
|
|
try {
|
|
secrets_raw = readFileSync(secrets_path, 'utf8');
|
|
} catch (err) {
|
|
throw new Error(`Cannot read secrets file at ${secrets_path}: ${err.message}`);
|
|
}
|
|
let secrets;
|
|
try {
|
|
secrets = JSON.parse(secrets_raw);
|
|
} catch (err) {
|
|
throw new Error(`Secrets file is not valid JSON: ${err.message}`);
|
|
}
|
|
if (!secrets.users || typeof secrets.users !== 'object') {
|
|
throw new Error("Secrets file must have a top-level 'users' object");
|
|
}
|
|
|
|
const mail_perms_path = parsed.mail_perms
|
|
? resolve(config_dir, parsed.mail_perms)
|
|
: null;
|
|
|
|
return {
|
|
users: secrets.users,
|
|
smtp: parsed.smtp ?? null,
|
|
mail_perms_path,
|
|
bind: parsed.bind ?? null,
|
|
port: parsed.port ?? null,
|
|
};
|
|
}
|