Allow overriding config.yaml values with env vars Closes #3520
| @@ -261,7 +261,7 @@ app.use(responseTime()); | |||
| 261 | 261 | ||
| 262 | 262 | ||
| 263 | /** @type {number} */ | 263 | /** @type {number} */ |
| 264 | const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getConfigValue('port', DEFAULT_PORT); | 264 | const server_port = cliArguments.port ?? getConfigValue('port', DEFAULT_PORT); |
| 265 | /** @type {boolean} */ | 265 | /** @type {boolean} */ |
| 266 | const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl; | 266 | const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl; |
| 267 | /** @type {boolean} */ | 267 | /** @type {boolean} */ |
| @@ -21,6 +21,14 @@ import { LOG_LEVELS } from './constants.js'; | |||
| 21 | let CACHED_CONFIG = null; | 21 | let CACHED_CONFIG = null; |
| 22 | 22 | ||
| 23 | /** | 23 | /** |
| 24 | * Converts a configuration key to an environment variable key. | ||
| 25 | * @param {string} key Configuration key | ||
| 26 | * @returns {string} Environment variable key | ||
| 27 | * @example keyToEnv('extensions.models.speechToText') // 'SILLYTAVERN_EXTENSIONS_MODELS_SPEECHTOTEXT' | ||
| 28 | */ | ||
| 29 | export const keyToEnv = (key) => 'SILLYTAVERN_' + String(key).toUpperCase().replace(/\./g, '_'); | ||
| 30 | |||
| 31 | /** | ||
| 24 | * Returns the config object from the config.yaml file. | 32 | * Returns the config object from the config.yaml file. |
| 25 | * @returns {object} Config object | 33 | * @returns {object} Config object |
| 26 | */ | 34 | */ |
| @@ -53,6 +61,10 @@ export function getConfig() { | |||
| 53 | * @returns {any} Value for the given key | 61 | * @returns {any} Value for the given key |
| 54 | */ | 62 | */ |
| 55 | export function getConfigValue(key, defaultValue = null) { | 63 | export function getConfigValue(key, defaultValue = null) { |
| 64 | const envKey = keyToEnv(key); | ||
| 65 | if (envKey in process.env) { | ||
| 66 | return process.env[envKey]; | ||
| 67 | } | ||
| 56 | const config = getConfig(); | 68 | const config = getConfig(); |
| 57 | return _.get(config, key, defaultValue); | 69 | return _.get(config, key, defaultValue); |
| 58 | } | 70 | } |