refactor webserver code

This commit is contained in:
Iuri Matias 2017-12-19 09:50:29 -05:00
parent 76be683ed2
commit 799da089af
2 changed files with 25 additions and 31 deletions

View File

@ -162,7 +162,7 @@ class Engine {
watch.start(); watch.start();
} }
webServerService(options) { webServerService() {
this.registerModule('webserver', { this.registerModule('webserver', {
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor) addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor)
}); });

View File

@ -7,49 +7,43 @@ class WebServer {
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.addCheck = options.addCheck; this.addCheck = options.addCheck;
this.webServerConfig = embark.config.webServerConfig;
let self = this; if (!this.webServerConfig.enabled) {
let webServerConfig = embark.config.webServerConfig;
if (!webServerConfig.enabled) {
return; return;
} }
let host = options.host || webServerConfig.host; this.host = options.host || this.webServerConfig.host;
let port = options.port || webServerConfig.port; this.port = options.port || this.webServerConfig.port;
this.logger.setStatus("Starting Server"); this.logger.setStatus("Starting Server");
let server = new Server({ this.server = new Server({logger: this.logger, host: this.host, port: this.port});
logger: this.logger,
host: host, this.setServiceCheck();
port: port this.listenToCommands();
});
this.server.start();
}
setServiceCheck() {
let url = 'http://' + this.host + ':' + this.port;
//embark.registerServiceCheck('WebserverService', function (cb) { //embark.registerServiceCheck('WebserverService', function (cb) {
this.addCheck('Webserver', function (cb) { this.addCheck('Webserver', function (cb) {
utils.checkIsAvailable('http://' + host + ':' + port, function (available) { utils.checkIsAvailable(url, function (available) {
let devServer = 'Webserver (http://' + host + ':' + port + ')'; let devServer = 'Webserver (' + url + ')';
if (available) { let serverStatus = (available ? 'on' : 'off');
return cb({name: devServer, status: 'on'}); return cb({name: devServer, status: serverStatus});
} else {
return cb({name: devServer, status: 'off'});
}
}); });
}); });
self.events.on('check:wentOffline:Webserver', function () { this.events.on('check:wentOffline:Webserver', () => {
self.logger.info("Webserver is offline"); this.logger.info("Webserver is offline");
}); });
}
self.events.setCommandHandler('start-webserver', function() { listenToCommands() {
server.start(); this.events.setCommandHandler('start-webserver', () => { this.server.start() });
}); this.events.setCommandHandler('stop-webserver', () => { this.server.stop() });
self.events.setCommandHandler('stop-webserver', function() {
server.stop();
});
server.start(function () {
});
} }
} }