Some checks failed
/ checks (push) Successful in 35s
/ deploy (push) Failing after 2s
39 lines
968 B
TypeScript
39 lines
968 B
TypeScript
import { Router } from "express";
|
|
import { hostname } from "node:os";
|
|
import type { Config } from "../lib/config.js";
|
|
import type { Db } from "../lib/db.js";
|
|
import type { Cache } from "../lib/redis.js";
|
|
|
|
export function rootRouter(deps: {
|
|
config: Config;
|
|
db?: Db;
|
|
cache?: Cache;
|
|
}): Router {
|
|
const router = Router();
|
|
|
|
router.get("/", async (_req, res, next) => {
|
|
try {
|
|
const body: Record<string, unknown> = {
|
|
service: deps.config.SERVICE_NAME,
|
|
version: deps.config.GIT_SHA,
|
|
pod: hostname(),
|
|
};
|
|
if (deps.db) {
|
|
const r = await deps.db.query(
|
|
"SELECT id, name, created_at FROM service_info ORDER BY id LIMIT 1",
|
|
);
|
|
body.db = r.rows[0] ?? null;
|
|
}
|
|
if (deps.cache) {
|
|
body.cacheHits = await deps.cache.incr(
|
|
`${deps.config.SERVICE_NAME}:hits`,
|
|
);
|
|
}
|
|
res.json(body);
|
|
} catch (err) {
|
|
next(err);
|
|
}
|
|
});
|
|
|
|
return router;
|
|
}
|