mirror of https://github.com/embarklabs/embark.git
refactor webserver
This commit is contained in:
parent
6d077b1412
commit
76be683ed2
|
@ -7,7 +7,6 @@ let DeployManager = require('../contracts/deploy_manager.js');
|
|||
let CodeGenerator = require('../contracts/code_generator.js');
|
||||
let ServicesMonitor = require('./services_monitor.js');
|
||||
let Pipeline = require('../pipeline/pipeline.js');
|
||||
let Server = require('../pipeline/server.js');
|
||||
let Watch = require('../pipeline/watch.js');
|
||||
|
||||
class Engine {
|
||||
|
@ -36,6 +35,9 @@ class Engine {
|
|||
startMonitor() {
|
||||
let self = this;
|
||||
if (this.plugins) {
|
||||
// --------
|
||||
// TODO: this only works for services done on startup
|
||||
// --------
|
||||
let servicePlugins = this.plugins.getPluginsFor('serviceChecks');
|
||||
servicePlugins.forEach(function (plugin) {
|
||||
plugin.serviceChecks.forEach(function (pluginCheck) {
|
||||
|
@ -161,46 +163,8 @@ class Engine {
|
|||
}
|
||||
|
||||
webServerService(options) {
|
||||
let self = this;
|
||||
let webServerConfig = this.config.webServerConfig;
|
||||
if (!webServerConfig.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
let host = options.host || webServerConfig.host;
|
||||
let port = options.port || webServerConfig.port;
|
||||
|
||||
this.logger.setStatus("Starting Server");
|
||||
let server = new Server({
|
||||
logger: this.logger,
|
||||
host: host,
|
||||
port: port
|
||||
});
|
||||
|
||||
self.servicesMonitor.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'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
self.events.on('check:wentOffline:Webserver', function () {
|
||||
self.logger.info("Webserver is offline");
|
||||
});
|
||||
|
||||
self.events.setCommandHandler('start-webserver', function() {
|
||||
server.start();
|
||||
});
|
||||
|
||||
self.events.setCommandHandler('stop-webserver', function() {
|
||||
server.stop();
|
||||
});
|
||||
|
||||
server.start(function () {
|
||||
this.registerModule('webserver', {
|
||||
addCheck: this.servicesMonitor.addCheck.bind(this.servicesMonitor)
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -104,6 +104,7 @@ Plugin.prototype.registerConsoleCommand = function(cb) {
|
|||
this.pluginTypes.push('console');
|
||||
};
|
||||
|
||||
// TODO: this only works for services done on startup
|
||||
Plugin.prototype.registerServiceCheck = function(checkName, checkFn, time) {
|
||||
this.serviceChecks.push({checkName: checkName, checkFn: checkFn, time: time});
|
||||
this.pluginTypes.push('serviceChecks');
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
var utils = require('../../utils/utils.js');
|
||||
var Server = require('./server.js');
|
||||
|
||||
class WebServer {
|
||||
|
||||
constructor(embark, options) {
|
||||
this.logger = embark.logger;
|
||||
this.events = embark.events;
|
||||
this.addCheck = options.addCheck;
|
||||
|
||||
let self = this;
|
||||
let webServerConfig = embark.config.webServerConfig;
|
||||
if (!webServerConfig.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
let host = options.host || webServerConfig.host;
|
||||
let port = options.port || webServerConfig.port;
|
||||
|
||||
this.logger.setStatus("Starting Server");
|
||||
let server = new Server({
|
||||
logger: this.logger,
|
||||
host: host,
|
||||
port: 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'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
self.events.on('check:wentOffline:Webserver', function () {
|
||||
self.logger.info("Webserver is offline");
|
||||
});
|
||||
|
||||
self.events.setCommandHandler('start-webserver', function() {
|
||||
server.start();
|
||||
});
|
||||
|
||||
self.events.setCommandHandler('stop-webserver', function() {
|
||||
server.stop();
|
||||
});
|
||||
|
||||
server.start(function () {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = WebServer;
|
Loading…
Reference in New Issue