gancio-upstream/server/config.js

40 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-09-27 10:42:17 +02:00
const fs = require('fs')
const path = require('path')
2021-11-08 11:03:41 +01:00
const URL = require('url')
2021-09-27 10:42:17 +02:00
let config = {
2022-01-26 09:51:42 +01:00
status: 'SETUP',
2021-11-08 11:03:41 +01:00
baseurl: '',
hostname: '',
2021-09-27 10:42:17 +02:00
server: {
2021-10-25 11:05:45 +02:00
host: '0.0.0.0',
2021-09-27 10:42:17 +02:00
port: 13120
},
log_level: 'debug',
2021-10-18 15:46:38 +02:00
log_path: path.resolve(process.env.cwd || '', 'logs'),
2021-09-27 10:42:17 +02:00
db: {},
2021-10-18 15:46:38 +02:00
upload_path: path.resolve(process.env.cwd || '', 'uploads'),
2021-09-27 10:42:17 +02:00
write (config_path= process.env.config_path || './config.json') {
2022-01-26 09:51:42 +01:00
delete config.status
2021-09-27 10:42:17 +02:00
return fs.writeFileSync(config_path, JSON.stringify(config, null, 2))
},
load () {
// load configuration from file
const config_path = process.env.config_path || './config.json'
2021-09-30 11:15:21 +02:00
console.info(`> Reading configuration from: ${config_path}`)
2021-09-27 10:42:17 +02:00
if (fs.existsSync(config_path)) {
const configContent = fs.readFileSync(config_path)
config = Object.assign(config, JSON.parse(configContent))
2022-01-26 09:51:42 +01:00
config.status = 'READY'
2021-11-08 11:03:41 +01:00
if (!config.hostname) {
config.hostname = new URL.URL(config.baseurl).hostname
}
2021-09-27 10:42:17 +02:00
} else {
2022-01-26 09:51:42 +01:00
config.status = 'SETUP'
2021-09-30 11:15:21 +02:00
console.info('> Configuration file does not exists, running setup..')
2021-09-27 10:42:17 +02:00
}
}
}
module.exports = config