Skip to content

Commit 8cd9ba2

Browse files
committed
Log version check info when app is starting
Signed-off-by: Yukai Huang <[email protected]>
1 parent 334c81e commit 8cd9ba2

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ 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')
28+
const { versionCheckMiddleware, checkVersion } = require('./lib/web/middleware/checkVersion')
2929

3030
function createHttpServer () {
3131
if (config.useSSL) {
@@ -170,6 +170,7 @@ app.use(require('./lib/middleware/redirectWithoutTrailingSlashes'))
170170
app.use(require('./lib/middleware/codiMDVersion'))
171171

172172
if (config.autoVersionCheck) {
173+
checkVersion(app)
173174
app.use(versionCheckMiddleware)
174175
}
175176

lib/web/middleware/checkVersion.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
1414

1515
const rp = promisify(request)
1616

17-
exports.versionCheckMiddleware = async function (req, res, next) {
17+
exports.checkVersion = checkVersion
18+
/**
19+
* @param {Express.Application|Express.Request} ctx
20+
*/
21+
async function checkVersion (ctx) {
1822
if (lastCheckAt && (lastCheckAt + CHECK_TIMEOUT > Date.now())) {
19-
return next()
23+
return
2024
}
2125

2226
// update lastCheckAt whether the check would fail or not
@@ -31,17 +35,36 @@ exports.versionCheckMiddleware = async function (req, res, next) {
3135

3236
if (statusCode !== 200 || data.status === 'error') {
3337
logger.error('Version check failed.')
34-
return next()
38+
return
3539
}
3640

37-
req.app.locals.versionInfo.latest = data.latest
38-
req.app.locals.versionInfo.versionItem = req.app.locals.latest ? null : data.versionItem
41+
let locals = ctx.locals ? ctx.locals : ctx.app.locals
3942

40-
return next()
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+
52+
return
4153
} catch (err) {
4254
// ignore and skip version check
4355
logger.error('Version check failed.')
4456
logger.error(err)
45-
return next()
57+
58+
return
4659
}
4760
}
61+
62+
exports.versionCheckMiddleware = function (req, res, next) {
63+
checkVersion(req)
64+
.then(() => {
65+
next()
66+
})
67+
.catch((err) => {
68+
next()
69+
})
70+
}

0 commit comments

Comments
 (0)