From 664db71104bd1efbad1e962b6d6270c436057a07 Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 1 Sep 2021 23:24:43 -0400 Subject: [PATCH 1/2] get status with options --- src/app.ts | 1 + src/routes/getStatus.ts | 8 +++++--- test/cases/getStatus.ts | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/app.ts b/src/app.ts index 1fb7e57..1d39d8b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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) { diff --git a/src/routes/getStatus.ts b/src/routes/getStatus.ts index c53d9c4..1bf6b62 100644 --- a/src/routes/getStatus.ts +++ b/src/routes/getStatus.ts @@ -3,14 +3,16 @@ import {Logger} from "../utils/logger"; import {Request, Response} from "express"; export async function getStatus(req: Request, res: Response): Promise { + 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 = { 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); diff --git a/test/cases/getStatus.ts b/test/cases/getStatus.ts index 3b5f3ed..47cc92b 100644 --- a/test/cases/getStatus.ts +++ b/test/cases/getStatus.ts @@ -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)); + }); }); From 74626f8e3f2500a8b43a3d0ebba41bf1892f05cb Mon Sep 17 00:00:00 2001 From: Michael C Date: Wed, 1 Sep 2021 23:49:18 -0400 Subject: [PATCH 2/2] more endpoints! --- src/routes/getStatus.ts | 3 +++ test/cases/getStatus.ts | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/routes/getStatus.ts b/src/routes/getStatus.ts index 1bf6b62..b4f3dfc 100644 --- a/src/routes/getStatus.ts +++ b/src/routes/getStatus.ts @@ -3,6 +3,7 @@ import {Logger} from "../utils/logger"; import {Request, Response} from "express"; export async function getStatus(req: Request, res: Response): Promise { + const startTime = Date.now(); let value = req.params.value as string[] | string; value = Array.isArray(value) ? value[0] : value; try { @@ -11,6 +12,8 @@ export async function getStatus(req: Request, res: Response): Promise 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) { diff --git a/test/cases/getStatus.ts b/test/cases/getStatus.ts index 47cc92b..b8be1be 100644 --- a/test/cases/getStatus.ts +++ b/test/cases/getStatus.ts @@ -15,9 +15,11 @@ 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)); @@ -28,7 +30,6 @@ describe("getStatus", () => { .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(); }) @@ -40,7 +41,6 @@ describe("getStatus", () => { .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(); }) @@ -52,10 +52,32 @@ describe("getStatus", () => { .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)); }); + + 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)); + }); });