Let API run with default settings in dev mode.

If the configuration file is not found, either at its
default place ($HOME/etc/www/config.json) or wherever
indicated by the DOUGAL_API_CONFIG environment variable,
create a temporary default config and try to run with it.

On the other hand, if we are in production (NODE_ENV == production)
we exit with non-zero in the absence of a real configuration file.
This commit is contained in:
D. Berge
2020-08-09 10:50:53 +02:00
parent 4c5d29494c
commit a766a4dc6c

View File

@@ -4,26 +4,45 @@ const crypto = require('crypto');
const cfgPath = process.env.DOUGAL_API_CONFIG || (process.env.HOME+"/etc/www/config.json");
let config = {}
try {
if (!fs.existsSync(cfgPath)) {
if (process.env.NODE_ENV == "production") {
console.error("Configuration file not found", path.resolve(cfgPath));
process.exit(-1);
} else {
console.warn("Configuration file not found", path.resolve(cfgPath));
const buffer = Buffer.alloc(64);
crypto.randomFillSync(buffer, 0, buffer.length);
const secret = buffer.toString("base64");
const config = {
config = {
"jwt": {
secret
};
// fs.writeFileSync(cfgPath, JSON.stringify(config, null, 4));
console.warn("Autogenerated configuration file");
},
"db": {
"user": "postgres",
"host": "localhost",
"database": "dougal",
"port": 5432,
"connection_string": "host=localhost port=5432 dbname=dougal user=postgres"
}
}
console.warn("Autogenerated configuration file (NODE_ENV != production)");
}
} else {
const text = fs.readFileSync(cfgPath);
if (text) {
module.exports = JSON.parse(text);
config = JSON.parse(text);
return;
}
}
} catch (err) {
console.error(err);
}
module.exports = {};
module.exports = Object.freeze(config);