Allow overriding config.yaml values with env vars Closes #3520

f6fe5fea77b96b5d490b6af8aecab52059fdc4fb

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +13 -1Ignore whitespace
server.js+1 -1
@@ -261,7 +261,7 @@ app.use(responseTime());
261261
262262
263/** @type {number} */263/** @type {number} */
264const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getConfigValue('port', DEFAULT_PORT);264const server_port = cliArguments.port ?? getConfigValue('port', DEFAULT_PORT);
265/** @type {boolean} */265/** @type {boolean} */
266const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl;266const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl;
267/** @type {boolean} */267/** @type {boolean} */
src/util.js+12 -0
@@ -21,6 +21,14 @@ import { LOG_LEVELS } from './constants.js';
21let CACHED_CONFIG = null;21let CACHED_CONFIG = null;
2222
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 */
29export 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 object33 * @returns {object} Config object
26 */34 */
@@ -53,6 +61,10 @@ export function getConfig() {
53 * @returns {any} Value for the given key61 * @returns {any} Value for the given key
54 */62 */
55export function getConfigValue(key, defaultValue = null) {63export 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}