mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
get status with options
This commit is contained in:
parent
cfefb7c629
commit
664db71104
3 changed files with 42 additions and 3 deletions
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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));
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue