Skip to content

Commit 178be63

Browse files
committed
refactor: Extract http server to a factory function
1 parent bce02bf commit 178be63

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed

src/app.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import Discord from 'discord.js';
22
import DiscordRSS from 'discord.rss';
3-
import Http from 'http';
43

54
import { handleCommand } from './commands';
65
import { getConfig } from './config';
76
import { InvalidUsageError } from './types';
8-
import handleGithubWebhook from './handle-github-webhook';
7+
import createHttpServer from './http-server';
98

109
const client = new Discord.Client();
1110
const drss = new DiscordRSS.Client({
@@ -83,36 +82,8 @@ async function init() {
8382

8483
init().catch((err) => errors.push(err));
8584

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);
11586

116-
server.listen(getConfig('PORT'), () => {
87+
httpServer.listen(getConfig('PORT'), () => {
11788
console.log(`Server running!`);
11889
});

src/http-server.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Http from 'http';
2+
import handleGithubWebhook from './handle-github-webhook';
3+
import type { Client } from 'discord.js';
4+
5+
const BAD_REQUEST = 400;
6+
const OK = 200;
7+
8+
function createHttpServer(
9+
discordClient: Client,
10+
errors: Error[],
11+
warnings: string[],
12+
debugs: string[]
13+
) {
14+
return Http.createServer(async (req, res) => {
15+
if (req.url?.startsWith('/githubWebhook')) {
16+
const chunks = [];
17+
for await (const chunk of req) {
18+
chunks.push(chunk);
19+
}
20+
21+
try {
22+
const body = JSON.parse(Buffer.concat(chunks).toString());
23+
24+
const { statusCode } = await handleGithubWebhook(req.url, body);
25+
26+
res.statusCode = statusCode;
27+
res.end();
28+
} catch (error) {
29+
console.log(error);
30+
res.statusCode = BAD_REQUEST;
31+
res.end();
32+
}
33+
34+
return;
35+
}
36+
37+
res.statusCode = OK;
38+
res.setHeader('Content-Type', 'application/json');
39+
res.end(JSON.stringify({ uptime: discordClient.uptime, errors, warnings, debugs }));
40+
});
41+
}
42+
43+
export default createHttpServer;

0 commit comments

Comments
 (0)