mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 12:27:07 +00:00
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:
@@ -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)) {
|
||||
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 = {
|
||||
secret
|
||||
};
|
||||
// fs.writeFileSync(cfgPath, JSON.stringify(config, null, 4));
|
||||
console.warn("Autogenerated configuration file");
|
||||
}
|
||||
|
||||
const text = fs.readFileSync(cfgPath);
|
||||
if (text) {
|
||||
module.exports = JSON.parse(text);
|
||||
return;
|
||||
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");
|
||||
|
||||
config = {
|
||||
"jwt": {
|
||||
secret
|
||||
},
|
||||
"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) {
|
||||
config = JSON.parse(text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
module.exports = {};
|
||||
module.exports = Object.freeze(config);
|
||||
|
||||
Reference in New Issue
Block a user