Implement checkVersion middleware

Checks version on each request, terminating the application if version 
mdoes not match
master
Dessa Simpson 2020-11-29 13:30:27 -07:00
parent 35aa0914a2
commit 21650994d2
2 changed files with 22 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import session from "express-session";
import pg from "pg";
import pgSessionStore from "connect-pg-simple";
import fetch, { Response as FetchResponse } from "node-fetch";
import { log, LogLevel } from "./logging"
import cron from "./cron";
import db from "./db";
import errorHandler from "./errors";
@ -26,6 +27,7 @@ async function validateApiToken(session: Express.Session) {
}
const app = express();
app.use(version.checkVersionMiddleware);
app.use(express.static('public'));
app.use(express.urlencoded({extended: false}));
app.use(session({
@ -340,4 +342,8 @@ version.checkVersion().then(_ => app.listen(config.port, () => {
cron.run();
setInterval(cron.run,config.cronInterval);
console.log(`Listening on port ${config.port}`);
}));
}))
.catch((e) => {
log(LogLevel.ERROR,e)
process.exit(1);
});

View File

@ -1,4 +1,6 @@
import * as queries from "./queries";
import { log, LogLevel } from "./logging"
import express from "express";
import pg from "pg";
import db from "./db";
@ -10,6 +12,19 @@ export function getVersion() {
return `${versionMajor}.${versionMinor}.${versionPatch}`
}
export async function checkVersionMiddleware(_req: express.Request, _res: express.Response, next: express.NextFunction) {
try {
await checkVersion();
} catch (e) {
log(LogLevel.ERROR,e)
// Terminate the nodejs process with error. If restarted, the app will
// never start listening for new requests so this will not result in a
// restart loop.
process.exit(1);
}
next();
}
export async function checkVersion() {
var dbver = await db.query(queries.getDbVersion).then((result: pg.QueryResult) => result.rows[0]['get_version']);
if (dbver != `${versionMajor}.${versionMinor}`) {