From 08c140b035e526578e41f6958f3dd257ade49967 Mon Sep 17 00:00:00 2001 From: Moksh Jain Date: Sat, 29 Sep 2018 00:41:26 +0530 Subject: [PATCH] added webserver logging --- lib/modules/webserver/index.js | 16 ++++++++++++++++ lib/modules/webserver/server.js | 22 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 285f0f787..2def89c6f 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -103,6 +103,8 @@ class WebServer { this.events.setCommandHandler('open-browser', (cb) => this.openBrowser(cb)); this.events.setCommandHandler('start-webserver', (cb) => this.server.start(cb)); this.events.setCommandHandler('stop-webserver', (cb) => this.server.stop(cb)); + this.events.setCommandHandler('logs:webserver:turnOn', (cb) => this.server.enableLogging(cb)); + this.events.setCommandHandler('logs:webserver:turnOff', (cb) => this.server.disableLogging(cb)); } registerConsoleCommands() { @@ -127,6 +129,20 @@ class WebServer { process: (cb) => self.events.request('open-browser', cb) }; }); + + self.embark.registerConsoleCommand((cmd, _options) => { + return { + match: () => cmd === 'log webserver on', + process: (cb) => self.events.request('logs:webserver:turnOn', cb) + }; + }); + + self.embark.registerConsoleCommand((cmd, _options) => { + return { + match: () => cmd === 'log webserver off', + process: (cb) => self.events.request('logs:webserver:turnOff', cb) + }; + }); } buildPlaceholderPage(cb) { diff --git a/lib/modules/webserver/server.js b/lib/modules/webserver/server.js index 4cbfa7a08..938f64dd8 100644 --- a/lib/modules/webserver/server.js +++ b/lib/modules/webserver/server.js @@ -15,6 +15,17 @@ class Server { this.isFirstStart = true; this.opened = false; this.openBrowser = options.openBrowser; + this.logging = false; + } + + enableLogging(callback) { + this.logging = true; + return callback(null, __("Enabled Webserver Logs")); + } + + disableLogging(callback) { + this.logging = false; + return callback(null, __("Disabled Webserver Logs")); } start(callback) { @@ -32,7 +43,16 @@ class Server { this.app = express(); const expressWs = expressWebSocket(this.app); - + + // Assign Logging Function + this.app.use(function(req, res, next) { + if (self.logging) { + if (!req.headers.upgrade) { + console.log('Webserver> ' + req.method + " " + req.originalUrl); + } + } + next(); + }); this.app.use(main); this.app.use('/coverage', coverage); this.app.use(coverageStyle);