service-core/runner.mjs

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-09-01 17:31:38 +00:00
import { readFileSync } from 'fs'
2020-09-08 08:11:42 +00:00
import getLog from './core/log.mjs'
import lowdb from './core/db.mjs'
2020-09-07 00:47:53 +00:00
import Core from './core/core.mjs'
2020-09-08 08:11:42 +00:00
import Util from './core/util.mjs'
2020-09-01 17:31:38 +00:00
let config
try {
config = JSON.parse(readFileSync('./config.json'))
} catch (err) {
let logger = getLog('critical-error')
logger.fatal('Error opening config file')
logger.fatal('Make sure it is valid JSON')
logger.fatal(err)
logger.event.error('Unable to start, error in config.json: ' + err.message)
process.exit(10)
}
const log = getLog(config.name)
2020-09-07 00:47:53 +00:00
const close = function(err) {
if (err) {
log.fatal(err, 'App recorded a fatal error')
log.event.error('App recorded a fatal error: ' + err.message, null, function() {
process.exit(4)
2020-09-01 17:31:38 +00:00
})
2020-09-07 00:47:53 +00:00
return
}
log.warn('App asked to be restarted')
log.event.warn('App requested to be restarted', null, function() {
process.exit(0)
2020-09-01 17:31:38 +00:00
})
2020-09-07 00:47:53 +00:00
}
2020-09-08 08:11:42 +00:00
const util = new Util(import.meta.url)
lowdb(util, log).then(function(db) {
let core = new Core(util, config, db, log, close)
2020-09-07 00:47:53 +00:00
if (config.useDev) {
return import('./dev/index.mjs').then(function(module) {
return module.start(config, db, log, core)
})
}
2020-09-01 17:31:38 +00:00
}, function(err) {
log.fatal(err, 'Critical error opening database')
2020-09-07 00:47:53 +00:00
log.event.error('Critical error opening database: ' + err.message, null, function() {
process.exit(2)
})
2020-09-01 17:31:38 +00:00
}).catch(function(err) {
log.fatal(err, 'Unknown error occured opening app')
2020-09-07 00:47:53 +00:00
log.event.error('Unknown error occured opening app: ' + err.message, null, function() {
process.exit(3)
})
2020-09-01 17:31:38 +00:00
})