-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
62 lines (54 loc) · 2.09 KB
/
app.ts
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
import { inject, injectable } from 'inversify';
import winston from 'winston';
import ChatBot from './bot/chat-bot';
import SAContainer from './dependency-management/inversify.config';
import InjectionTypes from './dependency-management/types';
import Database from './database/database';
import Scheduler from './bot/scheduler';
import SocketServer, { ISocketServer } from './bot/overlay/socket.server';
import OverlayServer, { IOverlayServer } from './bot/overlay/overlay.server';
@injectable()
class App {
constructor(
@inject(ChatBot) private chatBot: ChatBot,
@inject(Database) private database: Database,
@inject(Scheduler) private scheduler: Scheduler,
@inject(SocketServer) private socketServer: ISocketServer,
@inject(OverlayServer) private overlayServer: IOverlayServer,
@inject(InjectionTypes.Logger) public logger: winston.Logger,
) {
this.logger.info(`** Application initialized **`);
}
async main(): Promise<void> {
this.logger.info(`** Bot application starting **`);
await Promise.all([
this.chatBot.start(),
this.database.connect(),
this.database.sync(),
this.socketServer.startServer(),
this.overlayServer.configure().listen(),
this.scheduler.scheduleChatEvents(),
]);
}
async exit(): Promise<void> {
await Promise
.all([
this.database.disconnect(),
this.chatBot.shutdown(),
])
.then(() => {
this.logger.info('Process Terminated (0)');
process.exit();
});
}
}
SAContainer.bind<App>(App).to(App).inSingletonScope();
const application = SAContainer.get<App>(App);
application.main()
.catch(reason => {
application.logger.error(`Process Terminated (-1): ${reason}`);
process.exit(1);
});
process.on('SIGINT', () => application.exit());
process.on('SIGQUIT', () => application.exit());
process.on('SIGTERM', () => application.exit());