From 799da089af3f50eea8888e43978729b1405680f1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Tue, 19 Dec 2017 09:50:29 -0500 Subject: [PATCH] refactor webserver code --- lib/core/engine.js | 2 +- lib/modules/webserver/index.js | 54 +++++++++++++++------------------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index a2282a49..e69f6aef 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -162,7 +162,7 @@ class Engine { watch.start(); } - webServerService(options) { + webServerService() { this.registerModule('webserver', { addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor) }); diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js index 026cd6c4..7d50a4a9 100644 --- a/lib/modules/webserver/index.js +++ b/lib/modules/webserver/index.js @@ -7,49 +7,43 @@ class WebServer { this.logger = embark.logger; this.events = embark.events; this.addCheck = options.addCheck; - - let self = this; - let webServerConfig = embark.config.webServerConfig; - if (!webServerConfig.enabled) { + this.webServerConfig = embark.config.webServerConfig; + if (!this.webServerConfig.enabled) { return; } - let host = options.host || webServerConfig.host; - let port = options.port || webServerConfig.port; + this.host = options.host || this.webServerConfig.host; + this.port = options.port || this.webServerConfig.port; this.logger.setStatus("Starting Server"); - let server = new Server({ - logger: this.logger, - host: host, - port: port - }); + this.server = new Server({logger: this.logger, host: this.host, port: this.port}); + + this.setServiceCheck(); + this.listenToCommands(); + + this.server.start(); + } + + setServiceCheck() { + let url = 'http://' + this.host + ':' + this.port; //embark.registerServiceCheck('WebserverService', function (cb) { this.addCheck('Webserver', function (cb) { - utils.checkIsAvailable('http://' + host + ':' + port, function (available) { - let devServer = 'Webserver (http://' + host + ':' + port + ')'; - if (available) { - return cb({name: devServer, status: 'on'}); - } else { - return cb({name: devServer, status: 'off'}); - } + utils.checkIsAvailable(url, function (available) { + let devServer = 'Webserver (' + url + ')'; + let serverStatus = (available ? 'on' : 'off'); + return cb({name: devServer, status: serverStatus}); }); }); - self.events.on('check:wentOffline:Webserver', function () { - self.logger.info("Webserver is offline"); + this.events.on('check:wentOffline:Webserver', () => { + this.logger.info("Webserver is offline"); }); + } - self.events.setCommandHandler('start-webserver', function() { - server.start(); - }); - - self.events.setCommandHandler('stop-webserver', function() { - server.stop(); - }); - - server.start(function () { - }); + listenToCommands() { + this.events.setCommandHandler('start-webserver', () => { this.server.start() }); + this.events.setCommandHandler('stop-webserver', () => { this.server.stop() }); } }