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();
}
webServerService(options) {
webServerService() {
this.registerModule('webserver', {
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor)
});

View File

@ -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() });
}
}