diff --git a/README.md b/README.md index 905df5baf8f19f2fb9ad625a9c799ab968c495c4..cc6cdbc1a1d25ab9b361ed7120c2bdbb07db1114 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Once the code is compiled, the server can be started with `node server.js` in th The camera server can read a path to a config file from the `CONFIG_PATH` environment variable. This config file has to be in the json format and should have [this structure](./camera-server/example-config.json). +Additionally each config key can be overwritten with an environment variable `CVH_CAMERA_CONFIG_<key>` (e.g. `CVH_CAMERA_CONFIG_cameraSlots` to overwrite the numbers of camera slots). + Below is a description of the config file's properties: * `port`: The port on which the server will listen. Defaults to `5000`. diff --git a/camera-server/src/config/config.ts b/camera-server/src/config/config.ts index c14023ffa61f658d8bbac86c425203e96cc0f9a7..4abb248d5954a406f0f9f482a9daa9ce2f6ea6e8 100644 --- a/camera-server/src/config/config.ts +++ b/camera-server/src/config/config.ts @@ -84,6 +84,33 @@ if (fileContent) { } } +const ENV_CONFIG_PREFIX = 'CVH_CAMERA_CONFIG_'; +Object.keys(process.env).forEach((envKey) => { + if (envKey.startsWith(ENV_CONFIG_PREFIX)) { + const configKey = envKey.slice(ENV_CONFIG_PREFIX.length); + if (indexableConfig.hasOwnProperty(configKey)) { + const expectedType = typeof indexableConfig[configKey]; + if (expectedType === 'string') { + indexableConfig[configKey] = process.env[envKey]!; + } else if (expectedType === 'number') { + const envValue = process.env[envKey]!; + const num = parseInt(envValue); + if (!isNaN(num)) { + indexableConfig[configKey] = num; + } else { + console.log( + `Error: Config key ${configKey} from environment could not be parsed to a number` + ); + } + } + } else { + console.log( + `Error: Unknown config key ${configKey} in environment` + ); + } + } +}); + const config = indexableConfig as Config; if (config.notifyPath && !path.isAbsolute(config.notifyPath) && configPath) { config.notifyPath = path.resolve(