Skip to content

Commit 1126523

Browse files
committed
Implement version check middleware
Signed-off-by: Yukai Huang <[email protected]>
1 parent eefcf28 commit 1126523

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var response = require('./lib/response')
2525
var models = require('./lib/models')
2626
var csp = require('./lib/csp')
2727

28+
const { versionCheckMiddleware } = require('./lib/web/middleware/checkVersion')
29+
2830
function createHttpServer () {
2931
if (config.useSSL) {
3032
const ca = (function () {
@@ -167,6 +169,10 @@ app.use(require('./lib/middleware/checkURIValid'))
167169
app.use(require('./lib/middleware/redirectWithoutTrailingSlashes'))
168170
app.use(require('./lib/middleware/codiMDVersion'))
169171

172+
if (config.autoVersionCheck) {
173+
app.use(versionCheckMiddleware)
174+
}
175+
170176
// routes need sessions
171177
// template files
172178
app.set('views', config.viewPath)

lib/config/default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,6 @@ module.exports = {
178178
// Generated id: "31-good-morning-my-friend---do-you-have-5"
179179
// 2nd appearance: "31-good-morning-my-friend---do-you-have-5-1"
180180
// 3rd appearance: "31-good-morning-my-friend---do-you-have-5-2"
181-
linkifyHeaderStyle: 'keep-case'
181+
linkifyHeaderStyle: 'keep-case',
182+
autoVersionCheck: true
182183
}

lib/web/middleware/checkVersion.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict'
2+
3+
const { promisify } = require('util')
4+
5+
const request = require('request')
6+
7+
const logger = require('../../logger')
8+
const config = require('../../config')
9+
10+
let lastCheckAt
11+
let latest = true
12+
let versionItem = null
13+
14+
const VERSION_CHECK_ENDPOINT = 'https://evangelion.codimd.dev/'
15+
const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
16+
17+
const rp = promisify(request)
18+
19+
exports.versionCheckMiddleware = async function (req, res, next) {
20+
if (lastCheckAt && (lastCheckAt + CHECK_TIMEOUT > Date.now())) {
21+
return next()
22+
}
23+
24+
// update lastCheckAt whether the check would fail or not
25+
lastCheckAt = Date.now()
26+
27+
try {
28+
const { statusCode, body: data } = await rp({
29+
url: `${VERSION_CHECK_ENDPOINT}?v=${config.version}`,
30+
method: 'GET',
31+
json: true
32+
})
33+
34+
if (statusCode !== 200 || data.status === 'error') {
35+
logger.error('Version check failed.')
36+
return next()
37+
}
38+
39+
latest = data.latest
40+
versionItem = latest ? null : data.versionItem
41+
42+
return next()
43+
} catch (err) {
44+
// ignore and skip version check
45+
logger.error('Version check failed.')
46+
logger.error(err)
47+
return next()
48+
}
49+
}
50+
51+
exports.versionItem = versionItem
52+
exports.outdated = outdated

0 commit comments

Comments
 (0)