Merge pull request #352 from mchangrh/getStatus

add getStatus and cases
This commit is contained in:
Ajay Ramachandran 2021-09-01 17:11:25 -04:00 committed by GitHub
commit cfefb7c629
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 1 deletions

View file

@ -35,6 +35,7 @@ import {postPurgeAllSegments} from "./routes/postPurgeAllSegments";
import {getUserID} from "./routes/getUserID";
import {getLockCategories} from "./routes/getLockCategories";
import {getLockCategoriesByHash} from "./routes/getLockCategoriesByHash";
import {getStatus } from "./routes/getStatus";
import ExpressPromiseRouter from "express-promise-router";
import { Server } from "http";
@ -164,6 +165,9 @@ function setupRoutes(router: Router) {
// get privacy protecting lock categories functions
router.get("/api/lockCategories/:prefix", getLockCategoriesByHash);
// get status
router.get("/api/status", getStatus);
if (config.postgres) {
router.get("/database", (req, res) => dumpDatabase(req, res, true));
router.get("/database.json", (req, res) => dumpDatabase(req, res, false));

View file

@ -3,10 +3,13 @@ import {initDb} from "./databases/databases";
import {createServer} from "./app";
import {Logger} from "./utils/logger";
import {startAllCrons} from "./cronjob";
import { getCommit } from "./utils/getCommit";
async function init() {
await initDb();
(global as any).HEADCOMMIT = config.mode === "development" ? "development"
: config.mode === "test" ? "test"
: getCommit() as string;
createServer(() => {
Logger.info(`Server started on port ${config.port}.`);

18
src/routes/getStatus.ts Normal file
View file

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

4
src/utils/getCommit.ts Normal file
View file

@ -0,0 +1,4 @@
import { execSync } from "child_process";
const gitCommand = "git rev-parse HEAD";
export const getCommit = ():string => execSync(gitCommand).toString().trim();

25
test/cases/getStatus.ts Normal file
View file

@ -0,0 +1,25 @@
import assert from "assert";
import fetch from "node-fetch";
import {Done, getbaseURL} from "../utils";
import {db} from "../../src/databases/databases";
let dbVersion: number;
describe("getStatus", () => {
before(async () => {
dbVersion = (await db.prepare("get", "SELECT key, value FROM config where key = ?", ["version"])).value;
});
it("Should be able to get status", (done: Done) => {
fetch(`${getbaseURL()}/api/status`)
.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.strictEqual(data.commit, "test");
assert.strictEqual(data.db, Number(dbVersion));
done();
})
.catch(err => done(err));
});
});

View file

@ -26,6 +26,9 @@ async function init() {
: "sqlite";
Logger.info(`Database Mode: ${dbMode}`);
// set commit at headCommit
(global as any).HEADCOMMIT = "test";
// Instantiate a Mocha instance.
const mocha = new Mocha();