d6327597 by Rafal Wilinski

Added HTTPS support, port deprecated

1 parent 3a5053e5
...@@ -16,8 +16,6 @@ Monitor can be configured by passing options object into `expressMonitor` constr ...@@ -16,8 +16,6 @@ Monitor can be configured by passing options object into `expressMonitor` constr
16 Default config: 16 Default config:
17 ``` 17 ```
18 path: '/status', 18 path: '/status',
19 socketPort: 41338, // Port for Socket.io communication,
20 useHttps: false, // Force HTTPS protocol instead of HTTP
21 spans: [{ 19 spans: [{
22 interval: 1, // Every second 20 interval: 1, // Every second
23 retention: 60 // Keep 60 datapoints in memory 21 retention: 60 // Keep 60 datapoints in memory
......
...@@ -136,7 +136,7 @@ ...@@ -136,7 +136,7 @@
136 Chart.defaults.global.elements.line.borderColor = "rgba(0,0,0,0.9)"; 136 Chart.defaults.global.elements.line.borderColor = "rgba(0,0,0,0.9)";
137 Chart.defaults.global.elements.line.borderWidth = 2; 137 Chart.defaults.global.elements.line.borderWidth = 2;
138 138
139 var socket = io('{{protocol}}'+ '://' + window.location.hostname + ':{{port}}'); 139 var socket = io(location.protocol + '//' + location.hostname + ':' + location.port);
140 var defaultSpan = 0; 140 var defaultSpan = 0;
141 var spans = []; 141 var spans = [];
142 142
......
...@@ -6,11 +6,10 @@ ...@@ -6,11 +6,10 @@
6 const os = require('os'); 6 const os = require('os');
7 const onHeaders = require('on-headers'); 7 const onHeaders = require('on-headers');
8 const pidusage = require('pidusage'); 8 const pidusage = require('pidusage');
9 let io;
9 10
10 const defaultConfig = { 11 const defaultConfig = {
11 socketPort: 41338,
12 path: '/status', 12 path: '/status',
13 useHttps: false,
14 spans: [{ 13 spans: [{
15 interval: 1, 14 interval: 1,
16 retention: 60 15 retention: 60
...@@ -73,42 +72,30 @@ ...@@ -73,42 +72,30 @@
73 config.path = defaultConfig.path; 72 config.path = defaultConfig.path;
74 } 73 }
75 74
76 if (config.socketPort === undefined || !config instanceof Number) {
77 config.socketPort = defaultConfig.socketPort;
78 }
79
80 if (config.spans === undefined || !config instanceof Array) { 75 if (config.spans === undefined || !config instanceof Array) {
81 config.spans = defaultConfig.spans; 76 config.spans = defaultConfig.spans;
82 } 77 }
83 78
84 const io = require('socket.io')(config.socketPort); 79 return (req, res, next) => {
85 80 if (io === null || io === undefined) {
86 fs.readFile(path.join(__dirname, 'index.html'), 'utf8', (err,data) => { 81
87 if (err) throw new Error(err); 82 io = require('socket.io')(req.socket.server);
88
89 var result = data.replace(/{{port}}/g, config.socketPort).replace(/{{protocol}}/g, config.useHttps ? 'https' : 'http');
90
91 fs.writeFile(path.join(__dirname, 'index.rendered.html'), result, 'utf8', (err) => {
92 if (err) throw new Error(err);
93 });
94 });
95
96 io.on('connection', (socket) => {
97 socket.emit('start', config.spans);
98 83
99 socket.on('change', function() { socket.emit('start', config.spans); }); 84 io.on('connection', (socket) => {
100 }); 85 socket.emit('start', config.spans);
86 socket.on('change', function() { socket.emit('start', config.spans); });
87 });
101 88
102 config.spans.forEach((span) => { 89 config.spans.forEach((span) => {
103 span.os = []; 90 span.os = [];
104 span.responses = []; 91 span.responses = [];
105 setInterval(() => gatherOsMetrics(io, span), span.interval * 1000); 92 setInterval(() => gatherOsMetrics(io, span), span.interval * 1000);
106 }); 93 });
94 }
107 95
108 return (req, res, next) => {
109 const startTime = process.hrtime(); 96 const startTime = process.hrtime();
110 if (req.path === config.path) { 97 if (req.path === config.path) {
111 res.sendFile(path.join(__dirname + '/index.rendered.html')); 98 res.sendFile(path.join(__dirname + '/index.html'));
112 } else { 99 } else {
113 onHeaders(res, () => { 100 onHeaders(res, () => {
114 const diff = process.hrtime(startTime); 101 const diff = process.hrtime(startTime);
......
1 { 1 {
2 "name": "express-status-monitor", 2 "name": "express-status-monitor",
3 "version": "0.0.8", 3 "version": "0.0.9",
4 "description": "Monitoring for Express-based Node applications", 4 "description": "Monitoring for Express-based Node applications",
5 "main": "app.js", 5 "main": "app.js",
6 "keywords": [ 6 "keywords": [
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!