mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-09-20 04:54:00 +02:00
Merge pull request #353 from mchangrh/statusOptions
get status with options
This commit is contained in:
commit
9cbea88f6f
3 changed files with 68 additions and 4 deletions
|
@ -170,6 +170,7 @@ function setupRoutes(router: Router) {
|
|||
router.get("/api/searchSegments", getSearchSegments);
|
||||
|
||||
// get status
|
||||
router.get("/api/status/:value", getStatus);
|
||||
router.get("/api/status", getStatus);
|
||||
|
||||
if (config.postgres) {
|
||||
|
|
|
@ -3,14 +3,19 @@ import {Logger} from "../utils/logger";
|
|||
import {Request, Response} from "express";
|
||||
|
||||
export async function getStatus(req: Request, res: Response): Promise<Response> {
|
||||
const startTime = Date.now();
|
||||
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),
|
||||
});
|
||||
startTime,
|
||||
processTime: Date.now() - startTime,
|
||||
};
|
||||
return value ? res.send(String(statusValues[value])) : res.send(statusValues);
|
||||
} catch (err) {
|
||||
Logger.error(err as string);
|
||||
return res.sendStatus(500);
|
||||
|
|
|
@ -15,9 +15,67 @@ describe("getStatus", () => {
|
|||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.json();
|
||||
assert.ok(data.uptime >= 1); // uptime should be greater than 1s
|
||||
assert.ok(data.uptime); // uptime should be greater than 1s
|
||||
assert.strictEqual(data.commit, "test");
|
||||
assert.strictEqual(data.db, Number(dbVersion));
|
||||
assert.ok(data.startTime);
|
||||
assert.ok(data.processTime >= 0);
|
||||
done();
|
||||
})
|
||||
.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();
|
||||
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();
|
||||
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();
|
||||
assert.strictEqual(Number(data), Number(dbVersion)); // commit should be test
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get startTime only", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/status/startTime`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.text();
|
||||
const now = Date.now();
|
||||
assert.ok(Number(data) <= now); // startTime should be more than now
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
|
||||
it("Should be able to get processTime only", (done: Done) => {
|
||||
fetch(`${getbaseURL()}/api/status/processTime`)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const data = await res.text();
|
||||
assert.ok(Number(data) >= 0);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
|
|
Loading…
Reference in a new issue