|
1 | 1 | import Discord from 'discord.js'; |
2 | 2 | import DiscordRSS from 'discord.rss'; |
3 | | -import Http from 'http'; |
4 | 3 |
|
5 | 4 | import { handleCommand } from './commands'; |
6 | 5 | import { getConfig } from './config'; |
7 | 6 | import { InvalidUsageError } from './types'; |
8 | | -import handleGithubWebhook from './handle-github-webhook'; |
| 7 | +import createHttpServer from './http-server'; |
9 | 8 |
|
10 | 9 | const client = new Discord.Client(); |
11 | 10 | const drss = new DiscordRSS.Client({ |
@@ -83,36 +82,8 @@ async function init() { |
83 | 82 |
|
84 | 83 | init().catch((err) => errors.push(err)); |
85 | 84 |
|
86 | | -const BAD_REQUEST = 400; |
87 | | -const server = Http.createServer(async (req, res) => { |
88 | | - if (req.url?.startsWith('/githubWebhook')) { |
89 | | - const chunks = []; |
90 | | - for await (const chunk of req) { |
91 | | - chunks.push(chunk); |
92 | | - } |
93 | | - |
94 | | - try { |
95 | | - const body = JSON.parse(Buffer.concat(chunks).toString()); |
96 | | - |
97 | | - const { statusCode } = await handleGithubWebhook(req.url, body); |
98 | | - |
99 | | - res.statusCode = statusCode; |
100 | | - res.end(); |
101 | | - } catch (error) { |
102 | | - console.log(error); |
103 | | - res.statusCode = BAD_REQUEST; |
104 | | - res.end(); |
105 | | - } |
106 | | - |
107 | | - return; |
108 | | - } |
109 | | - |
110 | | - // tslint:disable-next-line:no-magic-numbers |
111 | | - res.statusCode = 200; |
112 | | - res.setHeader('Content-Type', 'application/json'); |
113 | | - res.end(JSON.stringify({ uptime: client.uptime, errors, warnings, debugs })); |
114 | | -}); |
| 85 | +const httpServer = createHttpServer(client, errors, warnings, debugs); |
115 | 86 |
|
116 | | -server.listen(getConfig('PORT'), () => { |
| 87 | +httpServer.listen(getConfig('PORT'), () => { |
117 | 88 | console.log(`Server running!`); |
118 | 89 | }); |
0 commit comments