gancio-upstream/server/config.js

54 lines
1.7 KiB
JavaScript
Raw Permalink 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: {
2022-05-05 11:11:06 +02:00
host: process.env.GANCIO_HOST || '0.0.0.0',
port: process.env.GANCIO_PORT || 13120
2021-09-27 10:42:17 +02:00
},
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: {},
2023-02-17 21:56:31 +01:00
user_locale: path.resolve(process.env.cwd || '', 'user_locale'),
2021-10-18 15:46:38 +02:00
upload_path: path.resolve(process.env.cwd || '', 'uploads'),
proxy: {
protocol: process.env.GANCIO_PROXY_PROTOCOL || '',
hostname: process.env.GANCIO_PROXY_HOSTNAME || '',
host: process.env.GANCIO_PROXY_HOST || '',
port: process.env.GANCIO_PROXY_PORT || '',
auth: {
username: process.env.GANCIO_PROXY_USERNAME || '',
password: process.env.GANCIO_PROXY_PASSWORD || ''
},
headers: process.env.GANCIO_PROXY_HEADERS && JSON.parse(process.env.GANCIO_PROXY_HEADERS) || {}
},
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))
config.status = 'CONFIGURED'
if (!config.hostname && config.baseurl) {
2021-11-08 11:03:41 +01:00
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'
console.info('> Configuration file does not exists...')
2021-09-27 10:42:17 +02:00
}
}
}
2022-06-18 01:15:52 +02:00
config.load()
module.exports = config