Skip to content
Snippets Groups Projects
Commit 5bfb3339 authored by Simon Döring's avatar Simon Döring
Browse files

Add configuration through environment variables

parent eaac9412
Branches
No related tags found
No related merge requests found
......@@ -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`.
......
......@@ -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(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment