-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
81 lines (75 loc) · 2.08 KB
/
index.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { txRouter } from "../microservices/tx/tx.routes";
import { GoldRushDecoder } from "../services";
import { timestampParser } from "@covalenthq/client-sdk";
import cors from "cors";
import { config as dotenvConfig } from "dotenv";
import express, {
type Express,
type NextFunction,
type Request,
type Response,
} from "express";
dotenvConfig();
const app: Express = express();
app.use(cors());
app.use(express.json());
app.get("/api/v1/healthcheck", (_req: Request, res: Response) => {
const now = new Date();
res.json({
success: true,
timestamp: now.toISOString(),
uptime: process.uptime(),
});
});
app.use("/api/v1/tx", txRouter);
app.use("*", (_req: Request, res: Response) => {
res.status(404).json({
success: false,
message: "Not Found",
});
});
app.use(
(err: Error | any, _req: Request, res: Response, _next: NextFunction) => {
const now = new Date();
console.error(
"Server Error",
`${now.toISOString()}: ${timestampParser(now, "descriptive")}`,
err
);
if (err.errorCode) {
res.status(err.errorCode).json({
success: false,
message: `${err.name || "Server Error"}: ${err.message}`,
});
} else {
res.status(500).json({
success: false,
message: "Internal Server Error",
});
}
}
);
(async () => {
try {
await Promise.all([GoldRushDecoder.initDecoder()]);
const env: string = process.env.NODE_ENV || "development";
if (env !== "test") {
const port: number = +(process.env.PORT || 8080);
app.listen(port, () => {
console.info(
`Server listening on Port ${port} in the ${env} environment`
);
});
}
} catch (error) {
console.error(error);
process.exit(1);
}
})();
process.on("SIGINT", () => {
process.exit(0);
});
process.on("SIGHUP", () => {
process.exit(0);
});
export default app;