-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathorchestrator.js
73 lines (65 loc) · 1.91 KB
/
orchestrator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logger from './log/index.js'
import Config, { READY, UPDATE, ERROR } from './config/index.js'
import { Container } from './container/index.js'
import { Corsproxy, Health, Metrics, Info } from './servlet/index.js'
import { getCertOptions } from './cert.js'
export default class Orchestrator {
constructor (argv) {
// TODO IoC/DI
this.config = new Config(argv)
this.container = new Container()
const corsproxy = new Corsproxy()
const health = new Health()
const metrics = new Metrics()
const info = new Info()
const servlets = {
'/info': info,
'/health': health,
'/metrics': metrics,
'': corsproxy
}
this.config
.on(READY, ({ log, server: { host, port, secure }, rules }) => {
const { port: securePort } = secure
logger
.configure(log)
.info(`Config path=${this.config.path || '<empty>'}`)
.info('Config ready.')
corsproxy
.configure({ port, host, rules, securePort })
health
.configure({ corsproxy })
metrics
.configure({ corsproxy })
this.container
.configure({
host,
port,
servlets,
secure: getCertOptions(secure)
})
.then(c => c.start())
})
.on(UPDATE, ({ log, server: { host, port, secure }, rules }) => {
const { port: securePort } = secure
logger
.configure(log)
.warn('Config updated.')
this.container.configure({
host,
port,
secure: getCertOptions(secure)
})
corsproxy.configure({ host, port, rules, securePort })
})
.on(ERROR, error => {
if (this.container.online) {
logger.warn(error)
logger.warn('Container uses the previous valid config.')
} else {
logger.error(error)
}
})
.load()
}
}