feat: initial backend service template
Some checks failed
/ checks (push) Successful in 35s
/ deploy (push) Failing after 2s
Some checks failed
/ checks (push) Successful in 35s
/ deploy (push) Failing after 2s
This commit is contained in:
commit
2828a630a6
28 changed files with 6437 additions and 0 deletions
39
src/routes/root.ts
Normal file
39
src/routes/root.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue