-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
61 lines (42 loc) · 1.27 KB
/
main.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
import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import EventEmitter from 'events';
import Plugins from './plugins';
dotenv.config();
const port = process.env.PORT ?? 8000;
class App extends EventEmitter {
server: Express;
stopped: boolean = false;
plugins: Plugins;
constructor() {
super();
this.plugins = new Plugins(this);
this.server = express();
this.server.use(express.json())
}
async start() {
await this.plugins.loadPlugins()
this.server.get('/', (req: Request, res: Response) => {
res.send('Express + TypeScript Server');
});
this.server.listen(port, () => {
console.log(`⚡️[server]: Server is running at http://localhost:${port}`);
});
}
stop() {
if (this.stopped) return;
// stop plugins
this.plugins.stop()
console.log("Server stopped");
this.emit('stop');
this.stopped = true;
process.exit();
}
}
const app: App = new App();
app.start();
const stopEvents = ["exit", "SIGINT", "SIGUSR1", "SIGUSR2", "SIGTERM", "uncaughtException"];
stopEvents.forEach(event => {
process.on(event, () => app.stop());
});
export default App