-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (67 loc) · 2.03 KB
/
index.js
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
82
83
import { createHourlyPricePoint, initDatabase } from "./src/manage-db.js";
import { PrismaClient } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library.js";
import fastify from "fastify";
import fastifyCron from "fastify-cron";
import { register } from "./src/routes.js";
export const prisma = new PrismaClient();
export const app = fastify({
logger: {
level: "debug",
},
});
const fiveMinutes = "*/5 * * * *";
const thirtySeconds = "*/30 * * * * *";
app.register(fastifyCron, {
jobs: [
{
name: "fetch-new-price-data",
cronTime:
process.env["NODE_ENV"] === "development" ? thirtySeconds : fiveMinutes,
onTick: async (_) => createHourlyPricePoint(),
},
],
});
app.addHook("preHandler", (req, reply, done) => {
reply.header("Content-Type", "application/json");
reply.header("Cache-Control", "s-max-age=100, stale-while-revalidate");
done();
});
register(app, prisma);
const port = process.env.PORT || 3000;
const host = process.env.HOST || "localhost";
const start = async () => {
try {
await app.listen({ port, host });
app.log.info(`🚀 Server ready at: http://${host}:${port}`);
await initDatabase();
app.log.info("Successfully fetched daily data.");
} catch (err) {
if (err instanceof PrismaClientKnownRequestError) {
app.log.warn({ err }, "Failed to run init Database script");
return;
}
app.log.error({ err }, "Error starting server");
process.exit(1);
}
};
await start();
app.log.info("Starting cron jobs");
app.cron.startAllJobs();
process.on("uncaughtException", (err) => {
app.log.error({ err }, "Uncaugh Exception");
app.log.warn("Shutting down server because of uncaught exception");
process.exit(1);
});
process.on("unhandledRejection", (reason, promise) => {
app.log.error(
{
error: reason,
},
"Unhandled Promise Rejection"
);
// need to log the promise without stringifying it to properly
// display all rejection info
app.log.warn({ promise });
process.exit(1);
});