forked from TricklePay/tricklepay-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
46 lines (39 loc) · 1.55 KB
/
Copy pathindex.ts
File metadata and controls
46 lines (39 loc) · 1.55 KB
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
import { createRpcServer } from "./chain/rpc.js";
import { loadConfig } from "./config.js";
import { disconnect } from "./db.js";
import { Poller } from "./indexer/poller.js";
import { logger } from "./logger.js";
import { rootRoutes } from "./routes/root.js";
import { metricsRoutes } from "./routes/metrics.js";
import { statusRoutes } from "./routes/status.js";
import { streamRoutes } from "./routes/streams.js";
import { buildServer } from "./server.js";
async function main(): Promise<void> {
const config = loadConfig();
const app = await buildServer(config);
await app.register(rootRoutes(config));
await app.register(streamRoutes);
await app.register(statusRoutes);
await app.register(metricsRoutes);
const poller = new Poller(createRpcServer(config), config, logger);
// The indexer runs in the background alongside the HTTP server. A failure
// here is logged rather than left to crash the process serving the API.
poller.start().catch((err) => logger.error({ err }, "indexer stopped"));
const shutdown = async (signal: string): Promise<void> => {
logger.info({ signal }, "shutting down");
poller.stop();
await app.close();
// Drain the Prisma connection pool cleanly before exiting.
await disconnect();
process.exit(0);
};
process.on("SIGINT", () => void shutdown("SIGINT"));
process.on("SIGTERM", () => void shutdown("SIGTERM"));
try {
await app.listen({ port: config.port, host: config.host });
} catch (err) {
logger.error({ err }, "failed to start server");
process.exit(1);
}
}
void main();