Skip to content

Commit d22ea95

Browse files
authored
Merge pull request #1399 from hackmdio/feature/version-check
Version check middleware & logging
2 parents 6f78c9a + c26a9f1 commit d22ea95

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

app.js

Lines changed: 11 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, checkVersion } = require('./lib/web/middleware/checkVersion')
29+
2830
function createHttpServer () {
2931
if (config.useSSL) {
3032
const ca = (function () {
@@ -167,6 +169,11 @@ 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+
checkVersion(app)
174+
app.use(versionCheckMiddleware)
175+
}
176+
170177
// routes need sessions
171178
// template files
172179
app.set('views', config.viewPath)
@@ -200,6 +207,10 @@ app.locals.authProviders = {
200207
email: config.isEmailEnable,
201208
allowEmailRegister: config.allowEmailRegister
202209
}
210+
app.locals.versionInfo = {
211+
latest: true,
212+
versionItem: null
213+
}
203214

204215
// Export/Import menu items
205216
app.locals.enableDropBoxSave = config.isDropboxEnable

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/config/environment.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,6 @@ module.exports = {
142142
allowPDFExport: toBooleanConfig(process.env.CMD_ALLOW_PDF_EXPORT),
143143
openID: toBooleanConfig(process.env.CMD_OPENID),
144144
defaultUseHardbreak: toBooleanConfig(process.env.CMD_DEFAULT_USE_HARD_BREAK),
145-
linkifyHeaderStyle: process.env.CMD_LINKIFY_HEADER_STYLE
145+
linkifyHeaderStyle: process.env.CMD_LINKIFY_HEADER_STYLE,
146+
autoVersionCheck: toBooleanConfig(process.env.CMD_AUTO_VERSION_CHECK)
146147
}

lib/web/middleware/checkVersion.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
12+
const VERSION_CHECK_ENDPOINT = 'https://evangelion.codimd.dev/'
13+
const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
14+
15+
const rp = promisify(request)
16+
17+
exports.checkVersion = checkVersion
18+
/**
19+
* @param {Express.Application|Express.Request} ctx
20+
*/
21+
async function checkVersion (ctx) {
22+
if (lastCheckAt && (lastCheckAt + CHECK_TIMEOUT > Date.now())) {
23+
return
24+
}
25+
26+
// update lastCheckAt whether the check would fail or not
27+
lastCheckAt = Date.now()
28+
29+
try {
30+
const { statusCode, body: data } = await rp({
31+
url: `${VERSION_CHECK_ENDPOINT}?v=${config.version}`,
32+
method: 'GET',
33+
json: true
34+
})
35+
36+
if (statusCode !== 200 || data.status === 'error') {
37+
logger.error('Version check failed.')
38+
return
39+
}
40+
41+
const locals = ctx.locals ? ctx.locals : ctx.app.locals
42+
43+
locals.versionInfo.latest = data.latest
44+
locals.versionInfo.versionItem = data.latest ? null : data.versionItem
45+
46+
if (!data.latest) {
47+
const { version, link } = data.versionItem
48+
49+
logger.warn(`Your CodiMD version is out of date! The latest version is ${version}. Please see what's new on ${link}.`)
50+
}
51+
} catch (err) {
52+
// ignore and skip version check
53+
logger.error('Version check failed.')
54+
logger.error(err)
55+
}
56+
}
57+
58+
exports.versionCheckMiddleware = function (req, res, next) {
59+
checkVersion(req)
60+
.then(() => {
61+
next()
62+
})
63+
.catch((err) => {
64+
logger.error(err)
65+
next()
66+
})
67+
}

0 commit comments

Comments
 (0)