1 |
rakin |
53 |
import DiscordClient from "../client/Client"; |
2 |
|
|
import express, { Application, Request, Response } from 'express'; |
3 |
|
|
import routes from './routes'; |
4 |
|
|
import router from "./Router"; |
5 |
|
|
import { readFileSync } from "fs"; |
6 |
|
|
import path from "path"; |
7 |
|
|
|
8 |
|
|
export default class Server { |
9 |
|
|
app: Application; |
10 |
|
|
port: number = 4000; |
11 |
|
|
guildData: { |
12 |
|
|
[key: string]: { |
13 |
|
|
token: string; |
14 |
|
|
} |
15 |
|
|
}; |
16 |
|
|
|
17 |
|
|
constructor(protected client: DiscordClient) { |
18 |
|
|
this.app = express(); |
19 |
|
|
this.guildData = JSON.parse(readFileSync(path.resolve(__dirname, '..', '..', 'config', 'apiguilds.json')).toString()); |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
verifyToken(guild: string, token: string): boolean { |
23 |
|
|
return this.guildData[guild] && this.guildData[guild].token === token; |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
validToken(token: string): boolean { |
27 |
|
|
for (const key in this.guildData) { |
28 |
|
|
if (this.guildData[key].token === token) |
29 |
|
|
return true; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
return false; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
registerRoutes() { |
36 |
|
|
this.app.all('/', (req: Request, res: Response) => { |
37 |
|
|
res.send("Server is up."); |
38 |
|
|
}); |
39 |
|
|
|
40 |
|
|
this.app.use('/api', router); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
run(port: number = 4000) { |
44 |
|
|
this.port = port; |
45 |
|
|
|
46 |
|
|
this.registerRoutes(); |
47 |
|
|
this.app.listen(port, () => console.log('Server listening at port ' + this.port)); |
48 |
|
|
} |
49 |
|
|
} |