101 lines
3.1 KiB
TypeScript
101 lines
3.1 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import type { IDcRouterOptions } from '../ts/classes.dcrouter.js';
|
|
|
|
/**
|
|
* Parses a comma-separated env var into a string array.
|
|
* Returns undefined if the env var is not set or empty.
|
|
*/
|
|
function parseCommaSeparated(envVar: string | undefined): string[] | undefined {
|
|
if (!envVar || envVar.trim() === '') return undefined;
|
|
return envVar.split(',').map((s) => s.trim()).filter(Boolean);
|
|
}
|
|
|
|
/**
|
|
* Parses a comma-separated env var into a number array.
|
|
* Returns undefined if the env var is not set or empty.
|
|
*/
|
|
function parseCommaSeparatedNumbers(envVar: string | undefined): number[] | undefined {
|
|
const parts = parseCommaSeparated(envVar);
|
|
if (!parts) return undefined;
|
|
return parts.map((s) => parseInt(s, 10)).filter((n) => !isNaN(n));
|
|
}
|
|
|
|
/**
|
|
* Builds IDcRouterOptions from environment variables for OCI container mode.
|
|
*
|
|
* If DCROUTER_CONFIG_PATH is set and the file exists, it is loaded as a JSON base config.
|
|
* Individual env vars are then applied as overrides on top.
|
|
*/
|
|
export function getOciContainerConfig(): IDcRouterOptions {
|
|
let options: IDcRouterOptions = {};
|
|
|
|
// Load JSON config file if specified
|
|
const configPath = process.env.DCROUTER_CONFIG_PATH;
|
|
if (configPath && plugins.fs.existsSync(configPath)) {
|
|
const raw = plugins.fs.readFileSync(configPath, 'utf8');
|
|
options = JSON.parse(raw);
|
|
console.log(`[OCI Container] Loaded config from ${configPath}`);
|
|
}
|
|
|
|
// Apply env var overrides
|
|
if (process.env.DCROUTER_BASE_DIR) {
|
|
options.baseDir = process.env.DCROUTER_BASE_DIR;
|
|
}
|
|
|
|
// TLS config
|
|
const tlsEmail = process.env.DCROUTER_TLS_EMAIL;
|
|
const tlsDomain = process.env.DCROUTER_TLS_DOMAIN;
|
|
if (tlsEmail || tlsDomain) {
|
|
options.tls = {
|
|
...options.tls,
|
|
contactEmail: tlsEmail || options.tls?.contactEmail || '',
|
|
...(tlsDomain ? { domain: tlsDomain } : {}),
|
|
};
|
|
}
|
|
|
|
// Network config
|
|
if (process.env.DCROUTER_PUBLIC_IP) {
|
|
options.publicIp = process.env.DCROUTER_PUBLIC_IP;
|
|
}
|
|
|
|
const proxyIps = parseCommaSeparated(process.env.DCROUTER_PROXY_IPS);
|
|
if (proxyIps) {
|
|
options.proxyIps = proxyIps;
|
|
}
|
|
|
|
// DNS config
|
|
const nsDomains = parseCommaSeparated(process.env.DCROUTER_DNS_NS_DOMAINS);
|
|
if (nsDomains) {
|
|
options.dnsNsDomains = nsDomains;
|
|
}
|
|
|
|
const dnsScopes = parseCommaSeparated(process.env.DCROUTER_DNS_SCOPES);
|
|
if (dnsScopes) {
|
|
options.dnsScopes = dnsScopes;
|
|
}
|
|
|
|
// Email config
|
|
const emailHostname = process.env.DCROUTER_EMAIL_HOSTNAME;
|
|
const emailPorts = parseCommaSeparatedNumbers(process.env.DCROUTER_EMAIL_PORTS);
|
|
if (emailHostname || emailPorts) {
|
|
options.emailConfig = {
|
|
...options.emailConfig,
|
|
...(emailHostname ? { hostname: emailHostname } : {}),
|
|
...(emailPorts ? { ports: emailPorts } : {}),
|
|
domains: options.emailConfig?.domains || [],
|
|
routes: options.emailConfig?.routes || [],
|
|
} as IDcRouterOptions['emailConfig'];
|
|
}
|
|
|
|
// Cache config
|
|
const cacheEnabled = process.env.DCROUTER_CACHE_ENABLED;
|
|
if (cacheEnabled !== undefined) {
|
|
options.cacheConfig = {
|
|
...options.cacheConfig,
|
|
enabled: cacheEnabled === 'true',
|
|
};
|
|
}
|
|
|
|
return options;
|
|
}
|