внешняя конфигурация

This commit is contained in:
Alexey Kasyanchuk
2017-08-24 08:53:39 +03:00
parent 2342035af7
commit 69a2ad1c54
4 changed files with 55 additions and 12 deletions

View File

@ -1,5 +1,5 @@
module.exports = {
indexer: true,
let config = {
indexer: false,
domain: 'ratsontheboat.org',
httpPort: 8095,
@ -34,3 +34,40 @@ module.exports = {
spaceQuota: false,
spaceDiskLimit: 7 * 1024 * 1024 * 1024,
}
const fs = require('fs');
const debug = require('debug')('config')
const configProxy = new Proxy(config, {
set: (target, prop, value, receiver) => {
target[prop] = value
if(!fs.existsSync('config.json'))
fs.writeFileSync('config.json', '{}')
fs.readFile('config.json', 'utf8', (err, data) => {
let obj = JSON.parse(data)
obj[prop] = value;
fs.writeFileSync('config.json', JSON.stringify(obj, null, 4), 'utf8');
debug('saving config.json:', prop, '=', value)
})
}
})
config.load = () => {
debug('loading configuration')
if(fs.existsSync('config.json'))
{
debug('finded configuration config.json')
const data = fs.readFileSync('config.json', 'utf8')
const obj = JSON.parse(data);
for(let prop in obj)
{
config[prop] = obj[prop]
debug('config.json:', prop, '=', obj[prop])
}
}
return configProxy
}
module.exports = configProxy.load()