From ba0b0331514fc83922a07cad263a6f476ff73858 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 7 Jun 2022 10:44:53 +0800 Subject: [PATCH 1/6] fix: `socketPath` not being used on server-side --- src/helpers/socket-io-init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/socket-io-init.js b/src/helpers/socket-io-init.js index d897070..bf01158 100644 --- a/src/helpers/socket-io-init.js +++ b/src/helpers/socket-io-init.js @@ -19,7 +19,7 @@ module.exports = (server, config) => { if (config.websocket !== null) { io = config.websocket; } else { - io = socketIo(server); + io = socketIo(server, { path: config.socketPath }); } io.on('connection', socket => { From 01630c727ffc40ef2713d131b1412fef4991c828 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 7 Jun 2022 14:25:12 +0800 Subject: [PATCH 2/6] fix: validation issue with `ignoreStartsWith` --- src/helpers/validate.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/validate.js b/src/helpers/validate.js index a623dd3..b1d6e0d 100644 --- a/src/helpers/validate.js +++ b/src/helpers/validate.js @@ -37,14 +37,14 @@ module.exports = config => { ? mungeChartVisibility(config.chartVisibility) : defaultConfig.chartVisibility; config.ignoreStartsWith = - typeof config.path === 'string' + typeof config.ignoreStartsWith === 'string' ? config.ignoreStartsWith : defaultConfig.ignoreStartsWith; config.healthChecks = Array.isArray(config.healthChecks) ? config.healthChecks - : defaultConfig.healthChecks + : defaultConfig.healthChecks; return config; }; From b9d5aedf4e5d18da0582c3573227f8bb7c7ffb63 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 7 Jun 2022 14:49:43 +0800 Subject: [PATCH 3/6] feat: auto-prepend slash to pathing config --- src/helpers/validate.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/helpers/validate.js b/src/helpers/validate.js index b1d6e0d..c657eed 100644 --- a/src/helpers/validate.js +++ b/src/helpers/validate.js @@ -14,14 +14,29 @@ module.exports = config => { return defaultConfig.chartVisibility; }; + const prependWithSlash = string => { + if (string && !string.startsWith('/')) { + return `/${string}`; + } + return string; + }; + config.title = typeof config.title === 'string' ? config.title : defaultConfig.title; config.theme = typeof config.theme === 'string' ? config.theme : defaultConfig.theme; config.path = - typeof config.path === 'string' ? config.path : defaultConfig.path; + prependWithSlash( + typeof config.path === 'string' + ? config.path + : defaultConfig.path + ); config.socketPath = - typeof config.socketPath === 'string' ? config.socketPath : defaultConfig.socketPath; + prependWithSlash( + typeof config.socketPath === 'string' + ? config.socketPath + : defaultConfig.socketPath + ); config.spans = typeof config.spans === 'object' ? config.spans : defaultConfig.spans; config.port = @@ -37,14 +52,21 @@ module.exports = config => { ? mungeChartVisibility(config.chartVisibility) : defaultConfig.chartVisibility; config.ignoreStartsWith = - typeof config.ignoreStartsWith === 'string' - ? config.ignoreStartsWith - : defaultConfig.ignoreStartsWith; + prependWithSlash( + typeof config.ignoreStartsWith === 'string' + ? config.ignoreStartsWith + : defaultConfig.ignoreStartsWith + ); config.healthChecks = Array.isArray(config.healthChecks) ? config.healthChecks : defaultConfig.healthChecks; + config.healthChecks.forEach(healthCheck => { + if (healthCheck.path) { + healthCheck.path = prependWithSlash(healthCheck.path); + } + }); return config; }; From 7fb40da23b1259ba3dc54f1850c34f44ec52545b Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 7 Jun 2022 16:55:42 +0800 Subject: [PATCH 4/6] feat: added socket.io namespace support --- README.md | 1 + src/helpers/default-config.js | 1 + src/helpers/socket-io-init.js | 3 +++ src/helpers/validate.js | 6 ++++++ src/middleware-wrapper.js | 1 + src/public/index.html | 1 + src/public/javascripts/app.js | 6 +++--- 7 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 70c965f..2ab756e 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ title: 'Express Status', // Default title theme: 'default.css', // Default styles path: '/status', socketPath: '/socket.io', // In case you use a custom path +namespace: '/', // socket.io namespace websocket: existingSocketIoInstance, spans: [{ interval: 1, // Every second diff --git a/src/helpers/default-config.js b/src/helpers/default-config.js index 563acf6..d7fa00c 100644 --- a/src/helpers/default-config.js +++ b/src/helpers/default-config.js @@ -3,6 +3,7 @@ module.exports = { theme: 'default.css', path: '/status', socketPath: '/socket.io', + namespace: '/', spans: [ { interval: 1, diff --git a/src/helpers/socket-io-init.js b/src/helpers/socket-io-init.js index bf01158..1f7d439 100644 --- a/src/helpers/socket-io-init.js +++ b/src/helpers/socket-io-init.js @@ -21,6 +21,9 @@ module.exports = (server, config) => { } else { io = socketIo(server, { path: config.socketPath }); } + if (typeof io.of === 'function') { + io = io.of(config.namespace); + } io.on('connection', socket => { if (config.authorize) { diff --git a/src/helpers/validate.js b/src/helpers/validate.js index c657eed..31ff6ed 100644 --- a/src/helpers/validate.js +++ b/src/helpers/validate.js @@ -31,6 +31,12 @@ module.exports = config => { ? config.path : defaultConfig.path ); + config.namespace = + prependWithSlash( + typeof config.namespace === 'string' + ? config.namespace + : defaultConfig.namespace + ); config.socketPath = prependWithSlash( typeof config.socketPath === 'string' diff --git a/src/middleware-wrapper.js b/src/middleware-wrapper.js index 34f0eaa..018a6a8 100644 --- a/src/middleware-wrapper.js +++ b/src/middleware-wrapper.js @@ -22,6 +22,7 @@ const middlewareWrapper = config => { title: validatedConfig.title, port: validatedConfig.port, socketPath: validatedConfig.socketPath, + namespace: validatedConfig.namespace, bodyClasses, script: fs.readFileSync(path.join(__dirname, '/public/javascripts/app.js')), style: fs.readFileSync(path.join(__dirname, '/public/stylesheets/', validatedConfig.theme)) diff --git a/src/public/index.html b/src/public/index.html index b69f163..9056449 100644 --- a/src/public/index.html +++ b/src/public/index.html @@ -108,6 +108,7 @@

{{status}}

diff --git a/src/public/javascripts/app.js b/src/public/javascripts/app.js index 151453b..5b3410d 100644 --- a/src/public/javascripts/app.js +++ b/src/public/javascripts/app.js @@ -2,7 +2,7 @@ eslint-disable no-plusplus, no-var, strict, vars-on-top, prefer-template, func-names, prefer-arrow-callback, no-loop-func */ -/* global Chart, location, document, port, socketPath, parseInt, io */ +/* global Chart, location, document, port, socketPath, namespace, parseInt, io */ 'use strict'; @@ -13,9 +13,9 @@ Chart.defaults.global.elements.line.backgroundColor = 'rgba(0,0,0,0)'; Chart.defaults.global.elements.line.borderColor = 'rgba(0,0,0,0.9)'; Chart.defaults.global.elements.line.borderWidth = 2; -var socket = io(location.protocol + '//' + location.hostname + ':' + (port || location.port), { +var socket = io(location.protocol + '//' + location.hostname + ':' + (port || location.port) + namespace, { path: socketPath, - transports: ["websocket"] + transports: ['websocket'] }); var defaultSpan = 0; var spans = []; From 2fd5e9e29cf535027ce8024d6b9a12848c882fa2 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 7 Jun 2022 17:19:48 +0800 Subject: [PATCH 5/6] refactor: re-order the config --- README.md | 9 ++++----- src/helpers/default-config.js | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2ab756e..b7c9aa6 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ theme: 'default.css', // Default styles path: '/status', socketPath: '/socket.io', // In case you use a custom path namespace: '/', // socket.io namespace -websocket: existingSocketIoInstance, +websocket: null, // In case you use an existing socket.io instance spans: [{ interval: 1, // Every second retention: 60 // Keep 60 datapoints in memory @@ -60,15 +60,14 @@ chartVisibility: { cpu: true, mem: true, load: true, - eventLoop: true, heap: true, + eventLoop: true, responseTime: true, rps: true, statusCodes: true }, -healthChecks: [], -ignoreStartsWith: '/admin' - +ignoreStartsWith: '/admin', +healthChecks: [] ``` ## Health Checks diff --git a/src/helpers/default-config.js b/src/helpers/default-config.js index d7fa00c..3a24a89 100644 --- a/src/helpers/default-config.js +++ b/src/helpers/default-config.js @@ -4,6 +4,8 @@ module.exports = { path: '/status', socketPath: '/socket.io', namespace: '/', + port: null, + websocket: null, spans: [ { interval: 1, @@ -18,8 +20,6 @@ module.exports = { retention: 60, }, ], - port: null, - websocket: null, iframe: false, chartVisibility: { cpu: true, From 8c7b77fc7d5a9ed947a10e6104226f2b23262b06 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Tue, 7 Jun 2022 17:34:04 +0800 Subject: [PATCH 6/6] docs: added missing `port` option to README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b7c9aa6..912ddd5 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ theme: 'default.css', // Default styles path: '/status', socketPath: '/socket.io', // In case you use a custom path namespace: '/', // socket.io namespace +port: null, // socket.io port websocket: null, // In case you use an existing socket.io instance spans: [{ interval: 1, // Every second