mirror of https://github.com/embarklabs/embark.git
refactor webserver code
This commit is contained in:
parent
76be683ed2
commit
799da089af
|
@ -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)
|
||||||
});
|
});
|
||||||
|
|
|
@ -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'});
|
});
|
||||||
|
|
||||||
|
this.events.on('check:wentOffline:Webserver', () => {
|
||||||
|
this.logger.info("Webserver is offline");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
self.events.on('check:wentOffline:Webserver', function () {
|
listenToCommands() {
|
||||||
self.logger.info("Webserver is offline");
|
this.events.setCommandHandler('start-webserver', () => { this.server.start() });
|
||||||
});
|
this.events.setCommandHandler('stop-webserver', () => { this.server.stop() });
|
||||||
|
|
||||||
self.events.setCommandHandler('start-webserver', function() {
|
|
||||||
server.start();
|
|
||||||
});
|
|
||||||
|
|
||||||
self.events.setCommandHandler('stop-webserver', function() {
|
|
||||||
server.stop();
|
|
||||||
});
|
|
||||||
|
|
||||||
server.start(function () {
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue