1 |
rakin |
323 |
import DiscordClient from "../client/Client"; |
2 |
|
|
import Service from "../utils/structures/Service"; |
3 |
|
|
import express, { Express } from 'express'; |
4 |
|
|
import Router from "./Router"; |
5 |
|
|
import path from "path"; |
6 |
|
|
|
7 |
|
|
export interface ServerOptions { |
8 |
|
|
port?: number; |
9 |
|
|
} |
10 |
|
|
|
11 |
|
|
export default class Server extends Service { |
12 |
|
|
port: number; |
13 |
|
|
express: Express; |
14 |
|
|
router: Router; |
15 |
|
|
|
16 |
|
|
constructor(client: DiscordClient, { port = 4000 }: ServerOptions = {}) { |
17 |
|
|
super(client); |
18 |
|
|
this.port = port; |
19 |
|
|
this.express = express(); |
20 |
|
|
this.router = new Router(client, this, { routesDir: path.resolve(__dirname, 'routes') }); |
21 |
|
|
} |
22 |
|
|
|
23 |
|
|
async boot() { |
24 |
|
|
await this.router.loadRoutes(); |
25 |
|
|
} |
26 |
|
|
|
27 |
|
|
async run() { |
28 |
|
|
await this.boot(); |
29 |
|
|
|
30 |
|
|
this.express.listen(this.port, () => { |
31 |
|
|
console.log(`API Server listening at port ${this.port}`); |
32 |
|
|
}); |
33 |
|
|
} |
34 |
|
|
} |