mirror of https://github.com/status-im/codimd.git
Implement version check middleware
Signed-off-by: Yukai Huang <yukaihuangtw@gmail.com>
This commit is contained in:
parent
eefcf28fcf
commit
11265238db
6
app.js
6
app.js
|
@ -25,6 +25,8 @@ var response = require('./lib/response')
|
||||||
var models = require('./lib/models')
|
var models = require('./lib/models')
|
||||||
var csp = require('./lib/csp')
|
var csp = require('./lib/csp')
|
||||||
|
|
||||||
|
const { versionCheckMiddleware } = require('./lib/web/middleware/checkVersion')
|
||||||
|
|
||||||
function createHttpServer () {
|
function createHttpServer () {
|
||||||
if (config.useSSL) {
|
if (config.useSSL) {
|
||||||
const ca = (function () {
|
const ca = (function () {
|
||||||
|
@ -167,6 +169,10 @@ app.use(require('./lib/middleware/checkURIValid'))
|
||||||
app.use(require('./lib/middleware/redirectWithoutTrailingSlashes'))
|
app.use(require('./lib/middleware/redirectWithoutTrailingSlashes'))
|
||||||
app.use(require('./lib/middleware/codiMDVersion'))
|
app.use(require('./lib/middleware/codiMDVersion'))
|
||||||
|
|
||||||
|
if (config.autoVersionCheck) {
|
||||||
|
app.use(versionCheckMiddleware)
|
||||||
|
}
|
||||||
|
|
||||||
// routes need sessions
|
// routes need sessions
|
||||||
// template files
|
// template files
|
||||||
app.set('views', config.viewPath)
|
app.set('views', config.viewPath)
|
||||||
|
|
|
@ -178,5 +178,6 @@ module.exports = {
|
||||||
// Generated id: "31-good-morning-my-friend---do-you-have-5"
|
// Generated id: "31-good-morning-my-friend---do-you-have-5"
|
||||||
// 2nd appearance: "31-good-morning-my-friend---do-you-have-5-1"
|
// 2nd appearance: "31-good-morning-my-friend---do-you-have-5-1"
|
||||||
// 3rd appearance: "31-good-morning-my-friend---do-you-have-5-2"
|
// 3rd appearance: "31-good-morning-my-friend---do-you-have-5-2"
|
||||||
linkifyHeaderStyle: 'keep-case'
|
linkifyHeaderStyle: 'keep-case',
|
||||||
|
autoVersionCheck: true
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const { promisify } = require('util')
|
||||||
|
|
||||||
|
const request = require('request')
|
||||||
|
|
||||||
|
const logger = require('../../logger')
|
||||||
|
const config = require('../../config')
|
||||||
|
|
||||||
|
let lastCheckAt
|
||||||
|
let latest = true
|
||||||
|
let versionItem = null
|
||||||
|
|
||||||
|
const VERSION_CHECK_ENDPOINT = 'https://evangelion.codimd.dev/'
|
||||||
|
const CHECK_TIMEOUT = 1000 * 60 * 60 * 24 // 1 day
|
||||||
|
|
||||||
|
const rp = promisify(request)
|
||||||
|
|
||||||
|
exports.versionCheckMiddleware = async function (req, res, next) {
|
||||||
|
if (lastCheckAt && (lastCheckAt + CHECK_TIMEOUT > Date.now())) {
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
// update lastCheckAt whether the check would fail or not
|
||||||
|
lastCheckAt = Date.now()
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { statusCode, body: data } = await rp({
|
||||||
|
url: `${VERSION_CHECK_ENDPOINT}?v=${config.version}`,
|
||||||
|
method: 'GET',
|
||||||
|
json: true
|
||||||
|
})
|
||||||
|
|
||||||
|
if (statusCode !== 200 || data.status === 'error') {
|
||||||
|
logger.error('Version check failed.')
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
|
||||||
|
latest = data.latest
|
||||||
|
versionItem = latest ? null : data.versionItem
|
||||||
|
|
||||||
|
return next()
|
||||||
|
} catch (err) {
|
||||||
|
// ignore and skip version check
|
||||||
|
logger.error('Version check failed.')
|
||||||
|
logger.error(err)
|
||||||
|
return next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.versionItem = versionItem
|
||||||
|
exports.outdated = outdated
|
Loading…
Reference in New Issue