From a1ab68f14440c7670470f891335561bc70298042 Mon Sep 17 00:00:00 2001 From: Michael Kreusel Date: Tue, 15 Jul 2025 16:42:09 +0200 Subject: [PATCH] feat(config): Make config elements overridable via environment variable Example for config file (smtp_password will get replaced with value of $SMTP_PASSWORD): ``` [nodemailer] smtp_server = "mail.example.com" smtp_port = "587" smtp_username = "noreply@example.com" smtp_password = "$SMTP_PASSWORD" ``` --- src/lib/config.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/config.ts b/src/lib/config.ts index ca6df769..1aca9c59 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -197,9 +197,20 @@ export const getConfig = (): GathioConfig => { } try { - const config = toml.parse( - fs.readFileSync("./config/config.toml", "utf-8"), - ) as GathioConfig; + const configFile = fs.readFileSync("./config/config.toml", "utf-8"); + const overriddenConfigFile = configFile.replace( + /\$\{?([a-zA-Z_-]+)}?/g, + (_ignored: string, envName: string) => { + const envValue = process.env[envName]; + if (!envValue) + exitWithError( + `Configuration env replacement not found, please set $${envName}`, + ); + + return String(envValue); + }, + ); + const config = toml.parse(overriddenConfigFile) as GathioConfig; const resolvedConfig = { ...defaultConfig, ...config,