const fs = require('fs').promises const path = require('path') const configFile = path.join(__dirname, '..', 'config.json') const defvals = { api: { host: '0.0.0.0', port: 7005, mount: '/api' }, rtmp: { host: 'icynet.eu:1935', mount: '/live', key: '' }, control: { host: '127.0.0.1', port: 7004 }, liquidsoap: { fallback: '', bitrate: 2000, entry: 'liq/view.liq' }, calendar: { googleKey: '', calendar: '', interval: 60, timeFrame: 120000, } } function getKeys (obj, vals) { let keys = {} for (let key in obj) { if (key === 'rtmp') { keys['rtmp.url'] = 'rtmp://' + vals.rtmp.host + vals.rtmp.mount keys['rtmp.key'] = vals.rtmp.key } else if (typeof vals[key] == 'object') { let ks = getKeys(defvals[key], vals[key]) for (let i in ks) { keys[key + '.' + i] = ks[i] } } else { keys[key] = vals[key] } } return keys } class Config { constructor () { for (let k in defvals) { this[k] = defvals[k] } } writeDefault () { fs.writeFile(configFile, JSON.stringify(defvals, null, 2)).catch((err) => { console.error(err.stack) process.exit(1) }) } async read () { try { await fs.stat(configFile) } catch (e) { return this.writeDefault() } let conf = await fs.readFile(configFile, 'utf-8') let json = JSON.parse(conf) let obj = Object.assign({}, defvals, json) for (let k in obj) { this[k] = obj[k] } } async liq () { await this.read() return JSON.stringify(getKeys(defvals, this)) } } module.exports = new Config()