|
|
|
@ -1,39 +1,96 @@
|
|
|
|
|
/**
|
|
|
|
|
* @module libflitter/app/FlitterApp
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const { Container, DependencyInjector } = require('flitter-di')
|
|
|
|
|
const Service = require('../NamedService')
|
|
|
|
|
const output = new (require('../utility/services/Output.service'))()
|
|
|
|
|
const Unit = require('../Unit')
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Flitter application.
|
|
|
|
|
* @extends module:flitter-di/src/Service~Service
|
|
|
|
|
*/
|
|
|
|
|
class FlitterApp extends Service {
|
|
|
|
|
/**
|
|
|
|
|
* Get the name of this app's service: 'app'
|
|
|
|
|
* @returns {string} - 'app'
|
|
|
|
|
*/
|
|
|
|
|
static get name() {
|
|
|
|
|
return 'app'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instantiate the application.
|
|
|
|
|
* @param {object} units - mapping of names to static Unit CLASS definitions
|
|
|
|
|
*/
|
|
|
|
|
constructor(units) {
|
|
|
|
|
super()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The app's units.
|
|
|
|
|
* @type {Array<module:libflitter/Unit~Unit>}
|
|
|
|
|
*/
|
|
|
|
|
this.__units = Array.isArray(units) ? units : Object.values(units)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Instance of the output service.
|
|
|
|
|
* @type {module:libflitter/utility/services/Output~Output}
|
|
|
|
|
*/
|
|
|
|
|
this.output = output
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of unit names.
|
|
|
|
|
* @type {Array<string>}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.__unit_array = []
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Mapping of directory names to paths.
|
|
|
|
|
* @type {object}
|
|
|
|
|
*/
|
|
|
|
|
this.directories = {}
|
|
|
|
|
|
|
|
|
|
this.__init_dependency_injector()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The underlying Express.js app.
|
|
|
|
|
* @type {express}
|
|
|
|
|
*/
|
|
|
|
|
this.express = require('express')()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Inject dependencies into the static class.
|
|
|
|
|
* @param {module:flitter-di/src/Injectable~Injectable} Class - the static CLASS
|
|
|
|
|
* @returns {*} - the injected CLASS
|
|
|
|
|
*/
|
|
|
|
|
make(Class) {
|
|
|
|
|
return this.__di.make(Class)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the DI.
|
|
|
|
|
* @returns {module:flitter-di/src/DependencyInjector~DependencyInjector}
|
|
|
|
|
*/
|
|
|
|
|
di() {
|
|
|
|
|
return this.__di
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run the application by starting, then cleanly stopping all units.
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
async run() {
|
|
|
|
|
await this.up()
|
|
|
|
|
await this.down()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the application by starting all units.
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
async up() {
|
|
|
|
|
this.output.message('Starting Flitter...', 0)
|
|
|
|
|
|
|
|
|
@ -44,6 +101,10 @@ class FlitterApp extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop the application cleanly by stopping all units.
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
*/
|
|
|
|
|
async down() {
|
|
|
|
|
this.output.message('Stopping flitter...', 0)
|
|
|
|
|
|
|
|
|
@ -56,6 +117,12 @@ class FlitterApp extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start a single unit.
|
|
|
|
|
* @param {module:libflitter/Unit~Unit} unit
|
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
async __init_unit(unit) {
|
|
|
|
|
this.output.info(`Starting ${unit.name()}...`)
|
|
|
|
|
|
|
|
|
@ -77,6 +144,12 @@ class FlitterApp extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Stop a single unit.
|
|
|
|
|
* @param {module:libflitter/Unit~Unit} unit
|
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
async __stop_unit(unit) {
|
|
|
|
|
this.output.info(`Stopping ${unit.name()}...`)
|
|
|
|
|
|
|
|
|
@ -92,6 +165,11 @@ class FlitterApp extends Service {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize the dependency injector for this app, adding the configured
|
|
|
|
|
* units to the container as services.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
__init_dependency_injector() {
|
|
|
|
|
const service_definitions = {
|
|
|
|
|
app: this.constructor
|
|
|
|
|