-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
66 lines (54 loc) · 1.77 KB
/
Copy pathserver.ts
File metadata and controls
66 lines (54 loc) · 1.77 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import express from "express";
import path from "path";
import { mysql, redis } from "./db/client";
import { startWorker } from "./worker/dispatcher";
import subscriptionsRouter from "./routes/subscriptions";
import eventsRouter from "./routes/events";
import dlqRouter from "./routes/dlq";
import jobsRouter from "./routes/jobs";
import { log } from "./lib/logger";
const app = express();
const port = process.env.PORT || 8787;
export { mysql, redis };
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.get("/health", async (req, res) => {
const [mysqlOk, redisOk] = await Promise.allSettled([
mysql`SELECT 1`,
redis.ping(),
]);
const [{ count }] = await mysql`
SELECT COUNT(*) as count FROM delivery_jobs WHERE status = 'pending'
`;
res.json({
status: "ok",
mysql: mysqlOk.status === "fulfilled" ? "connected" : "error",
redis: redisOk.status === "fulfilled" ? "connected" : "error",
queue_depth: Number(count),
});
});
app.use("/subscriptions", subscriptionsRouter);
app.use("/events", eventsRouter);
app.use("/dlq", dlqRouter);
app.use("/jobs", jobsRouter);
app.get("/api/test/mysql", async (req, res) => {
const rows = await mysql`SELECT 'test'`.values();
res.json({ data: rows });
});
app.get("/api/test/redis", async (req, res) => {
const pingresp = await redis.ping();
res.json({ data: pingresp });
});
async function migrate() {
const schema = await Bun.file("db/schema.sql").text();
for (const statement of schema.split(";").filter(s => s.trim())) {
await mysql.unsafe(statement);
}
}
async function main() {
log("info", "server.migrating");
await migrate();
log("info", "server.listening", { port });
startWorker(Number(process.env.WORKER_CONCURRENCY) || 5);
}
app.listen(port, main);