Repository navigation
Expand file tree
/
Copy pathApplication.ts
More file actions
53 lines (39 loc) · 1.52 KB
/
Copy pathApplication.ts
File metadata and controls
53 lines (39 loc) · 1.52 KB
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
import { Lincoln } from '@nofrills/scrubs'
import { DependencyContainer, container } from 'tsyringe'
import { LoggerType, Logger } from './Logger'
import { Config, Configuration } from './config'
import { Runnable, RunnableConstructor } from './Runnable'
export abstract class Application<TConfig extends Config> {
protected readonly container: DependencyContainer
constructor(
readonly name: string,
readonly configuration: Configuration<TConfig>,
private readonly server: RunnableConstructor,
) {
this.container = container.createChildContainer()
}
async run() {
const config = await this.setup()
const app = await this.build()
console.log(config)
process.on('SIGTERM', () => process.exit())
process.on('SIGHUP', () => process.exit())
process.on('SIGUSR1', () => process.exit())
process.on('SIGUSR2', () => process.exit())
process.on('uncaughtException', async error => console.error(error))
process.on('unhandledRejection', async error => console.error(error))
await app.initialize()
await this.configuration.save()
return app.start()
}
protected async build(): Promise<Runnable> {
return this.container.resolve(this.server)
}
protected async setup(): Promise<TConfig> {
const config = await this.configuration.load()
this.container.register<Lincoln>(LoggerType, { useValue: Logger })
this.dependencies(this.container, config)
return config
}
protected abstract dependencies(container: DependencyContainer, config: TConfig): void
}