File tree Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Expand file tree Collapse file tree 3 files changed +60
-1
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ var response = require('./lib/response')
25
25
var models = require ( './lib/models' )
26
26
var csp = require ( './lib/csp' )
27
27
28
+ const { versionCheckMiddleware } = require ( './lib/web/middleware/checkVersion' )
29
+
28
30
function createHttpServer ( ) {
29
31
if ( config . useSSL ) {
30
32
const ca = ( function ( ) {
@@ -167,6 +169,10 @@ app.use(require('./lib/middleware/checkURIValid'))
167
169
app . use ( require ( './lib/middleware/redirectWithoutTrailingSlashes' ) )
168
170
app . use ( require ( './lib/middleware/codiMDVersion' ) )
169
171
172
+ if ( config . autoVersionCheck ) {
173
+ app . use ( versionCheckMiddleware )
174
+ }
175
+
170
176
// routes need sessions
171
177
// template files
172
178
app . set ( 'views' , config . viewPath )
Original file line number Diff line number Diff line change @@ -178,5 +178,6 @@ module.exports = {
178
178
// Generated id: "31-good-morning-my-friend---do-you-have-5"
179
179
// 2nd appearance: "31-good-morning-my-friend---do-you-have-5-1"
180
180
// 3rd appearance: "31-good-morning-my-friend---do-you-have-5-2"
181
- linkifyHeaderStyle : 'keep-case'
181
+ linkifyHeaderStyle : 'keep-case' ,
182
+ autoVersionCheck : true
182
183
}
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments