diff --git a/lib/www/server/lib/config.js b/lib/www/server/lib/config.js index 46ad798..ed35d90 100644 --- a/lib/www/server/lib/config.js +++ b/lib/www/server/lib/config.js @@ -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);