Added dependency injection

This commit is contained in:
peter
2020-01-22 00:58:12 +01:00
parent 030908376b
commit e4a6972884
27 changed files with 1769 additions and 208 deletions
+30 -14
View File
@@ -3,19 +3,37 @@
import { getLogger } from 'frontblock-generic/Types';
import { promises as fs, mkdirSync } from "fs"
import { RPCServer } from 'rpclibrary'
import { AdminConf, TableDefiniton } from '../Types/Types';
import { RPCConfigLoader } from '../Components/RPCConfigLoader';
import * as Path from 'path'
import * as Knex from 'knex';
import * as http from 'http';
import * as express from 'express';
import { GuildManager } from '../Components/Guild/GuildManager';
import { ItemManager } from '../Components/Item/ItemManager';
import { RaidManager } from '../Components/Raid/RaidManager';
import { CharacterManager } from '../Components/User/CharacterManager';
import { LoginManager } from '../Components/User/LoginManager';
import { RootComponent } from '../Injector/ServiceDecorator';
import { TableDefinitionExporter } from '../Types/Interfaces';
import { AdminConf, TableDefiniton } from '../Types/Types';
import { RPCConfigLoader } from '../Components/RPCConfigLoader';
import { FrontworkComponent } from '../Types/FrontworkComponent';
import { IAdmin } from './Interface';
const logger = getLogger("admin", 'debug')
@RootComponent({
rootInterface: IAdmin,
imports: [
GuildManager,
ItemManager,
RaidManager,
CharacterManager,
LoginManager
]
})
export class FrontworkAdmin
implements TableDefinitionExporter {
implements TableDefinitionExporter, IAdmin {
knex:Knex
config: RPCConfigLoader<AdminConf>
rpcServer: RPCServer
@@ -23,7 +41,7 @@ implements TableDefinitionExporter {
private express
private httpServer
constructor(private components: FrontworkComponent[]){
constructor(private frontworkComponents: FrontworkComponent[] = []){
this.config = new RPCConfigLoader<AdminConf>({
name: "FrontworkAdminConf",
getDefaultConfig: () => {
@@ -40,19 +58,17 @@ implements TableDefinitionExporter {
}
}
}, './config', this.configChangeHandler)
components.forEach(c => {
c.admin = this
if(c.onSetAdmin) c.onSetAdmin(this)
})
}
async start(){
await this.makeKnex()
this.startWebsocket()
await Promise.all( this.components.map(c => c.initialize?c.initialize():undefined ))
await Promise.all( this.frontworkComponents.map(c => c.initialize?c.initialize():undefined ))
this.startWebserver()
}
stop(){
process.exit(0);
}
protected configChangeHandler = (conf:AdminConf, key?:string) => {
@@ -71,13 +87,13 @@ implements TableDefinitionExporter {
getTableDefinitions(): TableDefiniton[]{
return [
...this.components
...this.frontworkComponents
].flatMap(exporter => exporter.getTableDefinitions())
}
private startWebsocket(){
this.rpcServer = new RPCServer(20000, [
...this.components,
...this.frontworkComponents,
])
}
@@ -169,4 +185,4 @@ process.on( 'SIGINT', function() {
logger.info("Shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit(0);
})
})
+13
View File
@@ -0,0 +1,13 @@
import { RPCConfigLoader } from "../Components/RPCConfigLoader"
import { AdminConf } from "../Types/Types"
import { RPCServer } from "rpclibrary"
import Knex = require("knex")
export class IAdmin{
knex: Knex
config: RPCConfigLoader<AdminConf>
rpcServer: RPCServer
start: ()=>Promise<void>
stop: ()=>void
}