|
|
@@ -1,10 +1,9 @@
|
|
1
|
1
|
'use strict'
|
|
2
|
2
|
|
|
3
|
3
|
import { getLogger } from 'frontblock-generic/Types';
|
|
4
|
|
-import { ConfigLoader } from 'loadson';
|
|
5
|
|
-import { promises as fs } from "fs"
|
|
|
4
|
+import { promises as fs, mkdirSync } from "fs"
|
|
6
|
5
|
import { RPCServer } from 'rpclibrary/js/src/Backend'
|
|
7
|
|
-import { AdminConf } from './Types';
|
|
|
6
|
+import { AdminConf, TableDefiniton } from './Types';
|
|
8
|
7
|
import { RPCConfigLoader } from './RPCConfigLoader';
|
|
9
|
8
|
|
|
10
|
9
|
import * as Path from 'path'
|
|
|
@@ -12,20 +11,47 @@ import * as Path from 'path'
|
|
12
|
11
|
import Knex = require('knex');
|
|
13
|
12
|
import http = require('http');
|
|
14
|
13
|
import express = require('express');
|
|
|
14
|
+import { TableDefinitionExporter } from './Interfaces';
|
|
|
15
|
+import { FrontworkEventBus } from './Eventbus';
|
|
|
16
|
+import { Plugin } from './Plugin';
|
|
15
|
17
|
|
|
16
|
18
|
const logger = getLogger("admin", 'debug')
|
|
17
|
19
|
|
|
18
|
|
-export class FrontworkAdmin {
|
|
|
20
|
+export class FrontworkAdmin
|
|
|
21
|
+implements TableDefinitionExporter {
|
|
|
22
|
+
|
|
19
|
23
|
private express
|
|
20
|
24
|
private httpServer
|
|
21
|
|
- private config: RPCConfigLoader<AdminConf>
|
|
|
25
|
+ private eventBus:FrontworkEventBus = new FrontworkEventBus(this)
|
|
|
26
|
+ private plugins: Plugin[] = []
|
|
|
27
|
+ config: RPCConfigLoader<AdminConf>
|
|
|
28
|
+ knex:Knex
|
|
22
|
29
|
|
|
23
|
30
|
constructor(){
|
|
|
31
|
+ this.eventBus = new FrontworkEventBus(this)
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ async start(){
|
|
24
|
35
|
this.initConfig()
|
|
|
36
|
+ await this.makeKnex()
|
|
25
|
37
|
this.startWebsocket()
|
|
26
|
38
|
this.startWebserver()
|
|
27
|
39
|
}
|
|
28
|
40
|
|
|
|
41
|
+ protected configChangeHandler = (conf:AdminConf, key?:string) => {
|
|
|
42
|
+ if(key === 'dbConf'){
|
|
|
43
|
+ this.makeKnex()
|
|
|
44
|
+ }
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ getConfigKey(key:string){
|
|
|
48
|
+ return this.config.getConfigKey(key)
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ setConfigKey(key:string, value:any){
|
|
|
52
|
+ return this.config.setConfigKey(key, value)
|
|
|
53
|
+ }
|
|
|
54
|
+
|
|
29
|
55
|
private initConfig(){
|
|
30
|
56
|
this.config = new RPCConfigLoader<AdminConf>({
|
|
31
|
57
|
name: "FrontworkAdminConf",
|
|
|
@@ -42,13 +68,13 @@ export class FrontworkAdmin {
|
|
42
|
68
|
}
|
|
43
|
69
|
}
|
|
44
|
70
|
}
|
|
45
|
|
- }, './config', console.log)
|
|
|
71
|
+ }, './config', this.configChangeHandler)
|
|
46
|
72
|
}
|
|
47
|
73
|
|
|
48
|
74
|
private startWebsocket(){
|
|
49
|
|
- console.log()
|
|
50
|
75
|
new RPCServer(20000, [
|
|
51
|
|
- this.config
|
|
|
76
|
+ this.config,
|
|
|
77
|
+ ...this.plugins
|
|
52
|
78
|
])
|
|
53
|
79
|
}
|
|
54
|
80
|
|
|
|
@@ -108,6 +134,32 @@ export class FrontworkAdmin {
|
|
108
|
134
|
logger.info("Webserver stopped")
|
|
109
|
135
|
}
|
|
110
|
136
|
|
|
|
137
|
+ protected async makeKnex():Promise<Knex>{
|
|
|
138
|
+ const conf:Knex.Config = this.config.getConfigKey("dbConf")
|
|
|
139
|
+
|
|
|
140
|
+ logger.debug("Making new knex:", conf)
|
|
|
141
|
+ if(conf.client === 'sqlite3'){
|
|
|
142
|
+ mkdirSync(Path.dirname((<any>conf.connection).filename), {recursive: true})
|
|
|
143
|
+ }
|
|
|
144
|
+
|
|
|
145
|
+ this.knex = Knex(conf)
|
|
|
146
|
+
|
|
|
147
|
+ await Promise.all(this.getTableDefinitions().map(async (def)=>{
|
|
|
148
|
+ const hasTable = await this.knex.schema.hasTable(def.name)
|
|
|
149
|
+ if(!hasTable){
|
|
|
150
|
+ await this.knex.schema.createTable(def.name, def.tableBuilder)
|
|
|
151
|
+ }
|
|
|
152
|
+ }))
|
|
|
153
|
+
|
|
|
154
|
+ return this.knex
|
|
|
155
|
+ }
|
|
|
156
|
+
|
|
|
157
|
+ getTableDefinitions(): TableDefiniton[]{
|
|
|
158
|
+ return [
|
|
|
159
|
+ this.eventBus,
|
|
|
160
|
+ ...this.plugins
|
|
|
161
|
+ ].flatMap(exporter => exporter.getTableDefinitions())
|
|
|
162
|
+ }
|
|
111
|
163
|
}
|
|
112
|
164
|
|
|
113
|
165
|
process.on( 'SIGINT', function() {
|