forked from nmalzieu/starky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
58 lines (53 loc) · 1.6 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { createServer as createHttpServer } from "http";
import { createServer as createHttpsServer } from "http";
import next from "next";
import { parse } from "url";
import "reflect-metadata";
import { cleanStacks } from "./utils/execWithRateLimit";
import config from "./config";
import launchCron from "./cron";
import { setupDb } from "./db";
import { launchBot } from "./discord";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, hostname: config.HOST, port: config.PORT });
const handle = app.getRequestHandler();
// console.log(process.env);
// process.exit(1);
const launchServer = async () => {
// Setup the database
await setupDb();
// Prepare the next app
await app.prepare();
// Create the http server ready to receive requests
// TODO => handle HTTPS
const isHttps = false;
const createServer = isHttps ? createHttpsServer : createHttpServer;
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url || "", true);
await handle(req, res, parsedUrl);
} catch (err) {
console.error("Error occurred handling", req.url, err);
res.statusCode = 500;
res.end("internal server error");
}
}).listen(config.PORT, async () => {
console.log(
`> Server ready on http${isHttps ? "s" : ""}://${config.HOST}:${
config.PORT
}`
);
// Launch the Discord bot
try {
await launchBot();
} catch (e) {
throw new Error(`[Starky Discord Bot Error] ${e}`);
}
// Launch rate limit cleaner
cleanStacks();
// Launch the cron
launchCron();
});
};
launchServer();
export {};