1 |
rakinar2 |
577 |
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 pubRouter from "./PublicRouter"; |
6 |
|
|
import { readFileSync } from "fs"; |
7 |
|
|
import path from "path"; |
8 |
|
|
|
9 |
|
|
export default class Server { |
10 |
|
|
app: Application; |
11 |
|
|
port: number = 4000; |
12 |
|
|
guildData: { |
13 |
|
|
[key: string]: { |
14 |
|
|
token: string; |
15 |
|
|
} |
16 |
|
|
}; |
17 |
|
|
|
18 |
|
|
constructor(protected client: DiscordClient) { |
19 |
|
|
this.app = express(); |
20 |
|
|
const data: typeof this.guildData = {}; |
21 |
|
|
|
22 |
|
|
for (const key of Object.keys(process.env)) { |
23 |
|
|
if (key.startsWith('TOKEN_')) { |
24 |
|
|
data[key.replace(/^TOKEN_/g, '')] = { |
25 |
|
|
token: process.env[key]! |
26 |
|
|
}; |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
this.guildData = data; |
31 |
|
|
console.log(data); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
verifyToken(guild: string, token: string): boolean { |
35 |
|
|
return this.guildData[guild] && this.guildData[guild].token === token; |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
validToken(token: string): boolean { |
39 |
|
|
for (const key in this.guildData) { |
40 |
|
|
if (this.guildData[key].token === token) |
41 |
|
|
return true; |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
return false; |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
registerRoutes() { |
48 |
|
|
this.app.all('/', (req: Request, res: Response) => { |
49 |
|
|
res.send("Server is up."); |
50 |
|
|
}); |
51 |
|
|
|
52 |
|
|
this.app.use('/pub', pubRouter); |
53 |
|
|
this.app.use('/api', router); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
run(port: number = 4000) { |
57 |
|
|
this.port = port; |
58 |
|
|
|
59 |
|
|
this.registerRoutes(); |
60 |
|
|
this.app.listen(port, () => console.log('Server listening at port ' + this.port)); |
61 |
|
|
} |
62 |
|
|
} |