added webserver logging

This commit is contained in:
Moksh Jain 2018-09-29 00:41:26 +05:30 committed by Pascal Precht
parent 51b2cce0ea
commit 08c140b035
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 37 additions and 1 deletions

View File

@ -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) {

View File

@ -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) {
@ -33,6 +44,15 @@ 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);