From f6fe5fea77b96b5d490b6af8aecab52059fdc4fb Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 20 Feb 2025 20:29:42 +0200 Subject: [PATCH] Allow overriding config.yaml values with env vars Closes #3520 --- server.js | 2 +- src/util.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index cdef0a64b..933c10d5c 100644 --- a/server.js +++ b/server.js @@ -261,7 +261,7 @@ app.use(responseTime()); /** @type {number} */ -const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getConfigValue('port', DEFAULT_PORT); +const server_port = cliArguments.port ?? getConfigValue('port', DEFAULT_PORT); /** @type {boolean} */ const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl; /** @type {boolean} */ diff --git a/src/util.js b/src/util.js index 0ce6f66d4..27d6d2cf6 100644 --- a/src/util.js +++ b/src/util.js @@ -20,6 +20,14 @@ import { LOG_LEVELS } from './constants.js'; */ let CACHED_CONFIG = null; +/** + * Converts a configuration key to an environment variable key. + * @param {string} key Configuration key + * @returns {string} Environment variable key + * @example keyToEnv('extensions.models.speechToText') // 'SILLYTAVERN_EXTENSIONS_MODELS_SPEECHTOTEXT' + */ +export const keyToEnv = (key) => 'SILLYTAVERN_' + String(key).toUpperCase().replace(/\./g, '_'); + /** * Returns the config object from the config.yaml file. * @returns {object} Config object @@ -53,6 +61,10 @@ export function getConfig() { * @returns {any} Value for the given key */ export function getConfigValue(key, defaultValue = null) { + const envKey = keyToEnv(key); + if (envKey in process.env) { + return process.env[envKey]; + } const config = getConfig(); return _.get(config, key, defaultValue); }