|
|
@ -1,17 +1,19 @@ |
|
|
|
import DiscordBot from "./services/DiscordBot"; |
|
|
|
import services from "./services"; |
|
|
|
import Service from "./services/Service"; |
|
|
|
import TorrentClientIpc from "./services/TorrentClientIpc"; |
|
|
|
import WebServer from "./services/WebServer"; |
|
|
|
import { User, RegisterToken } from "./database/entities"; |
|
|
|
import Database from "./services/Database"; |
|
|
|
import assert from "assert"; |
|
|
|
|
|
|
|
let instance: Application; |
|
|
|
interface ServiceMap { |
|
|
|
[name: string]: Service |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* The main application class |
|
|
|
*/ |
|
|
|
export default class Application |
|
|
|
{ |
|
|
|
private static __instance: Application; |
|
|
|
|
|
|
|
/** |
|
|
|
* The application key used for signing stuff |
|
|
|
*/ |
|
|
@ -20,56 +22,40 @@ export default class Application |
|
|
|
/** |
|
|
|
* All available services |
|
|
|
*/ |
|
|
|
protected services: Service[]; |
|
|
|
|
|
|
|
/** |
|
|
|
* The database service |
|
|
|
*/ |
|
|
|
protected database!: Database; |
|
|
|
|
|
|
|
/** |
|
|
|
* The discord bot service |
|
|
|
*/ |
|
|
|
protected discord!: DiscordBot; |
|
|
|
|
|
|
|
/** |
|
|
|
* The webserver service |
|
|
|
*/ |
|
|
|
protected web!: WebServer; |
|
|
|
|
|
|
|
/** |
|
|
|
* The torrent client service |
|
|
|
*/ |
|
|
|
protected torrent!: TorrentClientIpc; |
|
|
|
protected services: ServiceMap = {}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Return the current application instance |
|
|
|
*/ |
|
|
|
public static instance() { |
|
|
|
return instance; |
|
|
|
return this.__instance; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new application instance |
|
|
|
*/ |
|
|
|
public constructor(appKey: string) { |
|
|
|
instance = this; |
|
|
|
Application.__instance = this; |
|
|
|
this.APP_KEY = appKey; |
|
|
|
this.services = [ |
|
|
|
this.database = new Database(this), |
|
|
|
// this.discord = new DiscordBot(this),
|
|
|
|
this.web = new WebServer(this), |
|
|
|
// this.torrent = new TorrentClientIpc(this)
|
|
|
|
] |
|
|
|
for (let ServiceClass of Object.values(services)) { |
|
|
|
this.installService(ServiceClass); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// this.torrent.logging = false;
|
|
|
|
/** |
|
|
|
* 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() { |
|
|
|
return Promise.all(this.services.map(service => service.boot())); |
|
|
|
let services = Object.values(this.services); |
|
|
|
return Promise.all(services.map(service => service.boot())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -91,7 +77,8 @@ export default class Application |
|
|
|
* Shutdown the application |
|
|
|
*/ |
|
|
|
protected shutdown() { |
|
|
|
return Promise.all(this.services.map(service => service.shutdown())); |
|
|
|
let services = Object.values(this.services); |
|
|
|
return Promise.all(services.map(service => service.shutdown())); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -100,7 +87,9 @@ export default class Application |
|
|
|
public async start() { |
|
|
|
await this.boot(); |
|
|
|
await this.initialize(); |
|
|
|
this.services.forEach(service => service.start()); |
|
|
|
for (let service of Object.values(this.services)) { |
|
|
|
service.start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -110,4 +99,21 @@ export default class Application |
|
|
|
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); |
|
|
|
return <T>this.services[serviceName]; |
|
|
|
} |
|
|
|
} |