mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
Merge pull request #352 from mchangrh/getStatus
add getStatus and cases
This commit is contained in:
commit
cfefb7c629
6 changed files with 58 additions and 1 deletions
|
@ -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));
|
||||
|
|
|
@ -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
18
src/routes/getStatus.ts
Normal 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
4
src/utils/getCommit.ts
Normal 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
25
test/cases/getStatus.ts
Normal 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));
|
||||
});
|
||||
});
|
|
@ -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();
|
||||
|
||||
|
|
Loading…
Reference in a new issue