get status with options

This commit is contained in:
Michael C 2021-09-01 23:24:43 -04:00
parent cfefb7c629
commit 664db71104
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
3 changed files with 42 additions and 3 deletions

View file

@ -166,6 +166,7 @@ function setupRoutes(router: Router) {
router.get("/api/lockCategories/:prefix", getLockCategoriesByHash);
// get status
router.get("/api/status/:value", getStatus);
router.get("/api/status", getStatus);
if (config.postgres) {

View file

@ -3,14 +3,16 @@ import {Logger} from "../utils/logger";
import {Request, Response} from "express";
export async function getStatus(req: Request, res: Response): Promise<Response> {
let value = req.params.value as string[] | string;
value = Array.isArray(value) ? value[0] : value;
try {
const dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
return res.send({
const statusValues: Record<string, any> = {
uptime: process.uptime(),
commit: (global as any).HEADCOMMIT || "unknown",
db: Number(dbVersion),
});
};
return value ? res.send(String(statusValues[value])) : res.send(statusValues);
} catch (err) {
Logger.error(err as string);
return res.sendStatus(500);

View file

@ -22,4 +22,40 @@ describe("getStatus", () => {
})
.catch(err => done(err));
});
it("Should be able to get uptime only", (done: Done) => {
fetch(`${getbaseURL()}/api/status/uptime`)
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
console.log(data);
assert.ok(Number(data) >= 1); // uptime should be greater than 1s
done();
})
.catch(err => done(err));
});
it("Should be able to get commit only", (done: Done) => {
fetch(`${getbaseURL()}/api/status/commit`)
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
console.log(data);
assert.strictEqual(data, "test"); // commit should be test
done();
})
.catch(err => done(err));
});
it("Should be able to get db only", (done: Done) => {
fetch(`${getbaseURL()}/api/status/db`)
.then(async res => {
assert.strictEqual(res.status, 200);
const data = await res.text();
console.log(data);
assert.strictEqual(Number(data), Number(dbVersion)); // commit should be test
done();
})
.catch(err => done(err));
});
});