2022-02-15 11:28:30 +00:00
|
|
|
import Util from './util.mjs'
|
|
|
|
import getLog from './log.mjs'
|
|
|
|
import GetDB from './db.mjs'
|
2022-03-10 13:08:02 +00:00
|
|
|
import Application from './application.mjs'
|
2022-02-15 11:28:30 +00:00
|
|
|
import StaticProvider from './providers/static.mjs'
|
|
|
|
import Core from './core.mjs'
|
|
|
|
|
|
|
|
export default class ServiceCore {
|
2022-03-10 13:38:35 +00:00
|
|
|
constructor(name, root_import_meta_url, port = 4000, dbfilename = 'db.json') {
|
2022-02-15 11:28:30 +00:00
|
|
|
if (!root_import_meta_url) {
|
|
|
|
throw new Error('ServiceCore must be called with the full string from "import.meta.url" from a file residing in the root directory')
|
|
|
|
}
|
|
|
|
this._root_import_meta_url = root_import_meta_url
|
|
|
|
this.util = new Util(this._root_import_meta_url)
|
2022-03-10 13:08:02 +00:00
|
|
|
this.dbfilename = dbfilename
|
2022-02-15 11:28:30 +00:00
|
|
|
this.log = getLog(name)
|
|
|
|
this.name = name
|
2022-03-27 16:02:53 +00:00
|
|
|
this.config = {
|
|
|
|
name: name,
|
|
|
|
title: 'Development Version of ' + name,
|
|
|
|
}
|
2022-02-15 11:28:30 +00:00
|
|
|
this.db = null
|
|
|
|
this.core = null
|
|
|
|
this.app = null
|
2022-03-10 13:38:35 +00:00
|
|
|
|
|
|
|
this.setConfig({
|
|
|
|
port: port,
|
|
|
|
})
|
2022-02-15 11:28:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 13:08:02 +00:00
|
|
|
setConfig(config) {
|
2022-03-27 16:02:53 +00:00
|
|
|
if (!config.provider) {
|
|
|
|
config.provider = 'static'
|
|
|
|
}
|
2022-03-10 13:08:02 +00:00
|
|
|
this.config[this.name] = config
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:28:30 +00:00
|
|
|
async init(module = null) {
|
|
|
|
this.db = await GetDB(this.config, this.log, this.dbfilename)
|
2022-03-29 17:14:50 +00:00
|
|
|
this.core = new Core(this.db, this.util, this.log, () => {
|
|
|
|
let err = new Error('Got request to restart')
|
|
|
|
this.log.fatal(err)
|
|
|
|
process.exit(0)
|
|
|
|
})
|
2022-02-15 11:28:30 +00:00
|
|
|
|
|
|
|
let provider = new StaticProvider()
|
|
|
|
this.app = new Application({
|
|
|
|
db: this.db,
|
|
|
|
util: this.util,
|
|
|
|
log: this.log,
|
|
|
|
core: this.core,
|
|
|
|
}, provider, this.name)
|
|
|
|
this.app.registerModule(module)
|
2022-03-27 16:02:53 +00:00
|
|
|
|
|
|
|
this.core.applications.push(this.app)
|
|
|
|
this.core.applicationMap.set(this.name, this.app)
|
2022-02-15 11:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run() {
|
|
|
|
return this.app.runVersion('static')
|
|
|
|
}
|
|
|
|
}
|