icytv-liq/control/config.js

105 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-12-18 17:38:18 +00:00
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,
2018-12-18 17:38:18 +00:00
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
}
2018-12-18 17:38:18 +00:00
},
calendar: {
googleKey: '',
calendar: '',
interval: 60,
timeFrame: 120000,
schedule: {
width: 300,
height: 400,
background: 'rgba(0,126,255,0.8)',
font: 'sans-serif',
fontColor: '#fff'
}
2018-12-18 17:38:18 +00:00
}
}
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') {
2018-12-18 17:38:18 +00:00
keys['rtmp.url'] = 'rtmp://' + vals.rtmp.host + vals.rtmp.mount
keys['rtmp.key'] = vals.rtmp.key
} else if (typeof vals[key] === 'object') {
2019-01-11 02:36:45 +00:00
let ks = getKeys(obj[key], vals[key])
2018-12-18 17:38:18 +00:00
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()