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: '', entry: 'liq/view.liq', output: { bitrate: 2000, width: 1280, height: 720, samplerate: 25 } }, calendar: { googleKey: '', calendar: '', interval: 60, timeFrame: 120000, schedule: { width: 300, height: 400, background: 'rgba(0,126,255,0.8)', font: 'sans-serif', fontColor: '#fff' } } } function getKeys (obj, vals) { let keys = {} for (let key in obj) { if (key === 'calendar') { keys['calendar.width'] = vals.calendar.schedule.width keys['calendar.height'] = vals.calendar.schedule.height } else 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(obj[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()