|
|
@ -1,104 +1,23 @@ |
|
|
|
import services from "./services"; |
|
|
|
import Service from "./services/Service"; |
|
|
|
import assert from "assert"; |
|
|
|
|
|
|
|
interface ServiceMap { |
|
|
|
[name: string]: Service |
|
|
|
} |
|
|
|
import { Microservice } from "@autoplex/microservice"; |
|
|
|
|
|
|
|
/** |
|
|
|
* The main application class |
|
|
|
*/ |
|
|
|
export default class Application |
|
|
|
export default class Application extends Microservice |
|
|
|
{ |
|
|
|
private static __instance: Application; |
|
|
|
|
|
|
|
/** |
|
|
|
* All available services |
|
|
|
*/ |
|
|
|
protected services: ServiceMap = {}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Return the current application instance |
|
|
|
*/ |
|
|
|
public static instance() { |
|
|
|
return this.__instance; |
|
|
|
} |
|
|
|
public static instance() { return super.instance() } |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new application instance |
|
|
|
*/ |
|
|
|
public constructor() { |
|
|
|
Application.__instance = this; |
|
|
|
super(); |
|
|
|
for (let ServiceClass of Object.values(services)) { |
|
|
|
this.installService(ServiceClass); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Install a service into the application |
|
|
|
*/ |
|
|
|
protected installService(ServiceClass: new (app: Application) => Service) { |
|
|
|
let service = new ServiceClass(this); |
|
|
|
this.services[service.name] = service; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Boot the application and all of the services |
|
|
|
*/ |
|
|
|
protected async boot() { |
|
|
|
let services = Object.values(this.services); |
|
|
|
return Promise.all(services.map(service => service.boot())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Initialize the application if necessary |
|
|
|
*/ |
|
|
|
protected async initialize() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Shutdown the application |
|
|
|
*/ |
|
|
|
protected shutdown() { |
|
|
|
let services = Object.values(this.services); |
|
|
|
return Promise.all(services.map(service => service.shutdown())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Start the application |
|
|
|
*/ |
|
|
|
public async start() { |
|
|
|
await this.boot(); |
|
|
|
await this.initialize(); |
|
|
|
for (let service of Object.values(this.services)) { |
|
|
|
service.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Quit the application |
|
|
|
*/ |
|
|
|
public async quit(code: number = 0) { |
|
|
|
await this.shutdown(); |
|
|
|
process.exit(code); |
|
|
|
} |
|
|
|
|
|
|
|
// Access --------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get all available services |
|
|
|
*/ |
|
|
|
public serviceList() { |
|
|
|
return Object.keys(this.services); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Get an application service instance |
|
|
|
*/ |
|
|
|
public service<T extends Service>(serviceName: string) { |
|
|
|
assert(serviceName in this.services, `Could not find service: ${serviceName}`); |
|
|
|
return <T>this.services[serviceName]; |
|
|
|
} |
|
|
|
} |