From c7f5a8cc6c7fe1beb2d355358f0202abd0e3171b Mon Sep 17 00:00:00 2001 From: buuzzy Date: Fri, 17 Apr 2026 09:24:22 +0800 Subject: [PATCH] feat: inject environment variables from config.json New env field in config.json for custom environment variables. Supports template syntax resolved from process.env. Only injects if the variable is not already set. --- src-api/src/config/loader.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src-api/src/config/loader.ts b/src-api/src/config/loader.ts index 6b6a691..f41f0bd 100644 --- a/src-api/src/config/loader.ts +++ b/src-api/src/config/loader.ts @@ -157,6 +157,7 @@ class ConfigLoader { this.mergeConfig(fileConfig, 'file'); this.configPath = absolutePath; + this.loadEnvFromConfig(fileConfig as unknown as Record); console.log(`[ConfigLoader] Loaded config from: ${absolutePath}`); } catch (error) { @@ -164,6 +165,24 @@ class ConfigLoader { } } + /** + * Load custom env vars from config.json's "env" field. + * Values like "${VAR}" are resolved from process.env. + */ + private loadEnvFromConfig(fileConfig: Record): void { + const envMap = fileConfig.env as Record | undefined; + if (!envMap || typeof envMap !== 'object') return; + + for (const [key, value] of Object.entries(envMap)) { + if (typeof value !== 'string') continue; + const resolved = value.replace(/\$\{(\w+)\}/g, (_, name) => process.env[name] || ''); + if (resolved && !process.env[key]) { + process.env[key] = resolved; + console.log(`[ConfigLoader] Injected env: ${key}`); + } + } + } + /** * Load configuration from environment variables */