-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathserver.ts
60 lines (54 loc) · 1.69 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
59
60
import { createServer as createHttpServer } from "http";
import { createServer as createHttpsServer } from "http";
import next from "next";
import { parse } from "url";
import "reflect-metadata";
import launchIndexers from "./indexer/indexer";
import { cleanStacks } from "./utils/execWithRateLimit";
import config from "./config";
import { setupDb } from "./db";
import { launchBot } from "./discord";
import WatchTowerLogger from "./watchTower";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev, hostname: config.HOST, port: config.PORT });
const handle = app.getRequestHandler();
const launchServer = async () => {
// Setup the database
await setupDb();
// Prepare the next app
await app.prepare();
// Create the http server ready to receive requests
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) {
WatchTowerLogger.error("Error occurred handling", { url: req.url, err });
res.statusCode = 500;
res.end("internal server error");
}
}).listen(config.PORT, async () => {
WatchTowerLogger.info(
`> 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
// Unused for now
//launchCron();
// Launch the Indexer
launchIndexers();
});
};
launchServer();
export {};