From 76be683ed21a4e51600e15fb4b662706c526ca18 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 18 Dec 2017 09:37:16 -0500 Subject: [PATCH] refactor webserver --- lib/core/engine.js | 46 ++------------- lib/core/plugin.js | 1 + lib/modules/webserver/index.js | 57 +++++++++++++++++++ lib/{pipeline => modules/webserver}/server.js | 0 4 files changed, 63 insertions(+), 41 deletions(-) create mode 100644 lib/modules/webserver/index.js rename lib/{pipeline => modules/webserver}/server.js (100%) diff --git a/lib/core/engine.js b/lib/core/engine.js index 8bb96a623..a2282a497 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -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) }); } diff --git a/lib/core/plugin.js b/lib/core/plugin.js index c7812e5c1..b53e57085 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -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'); diff --git a/lib/modules/webserver/index.js b/lib/modules/webserver/index.js new file mode 100644 index 000000000..026cd6c41 --- /dev/null +++ b/lib/modules/webserver/index.js @@ -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; diff --git a/lib/pipeline/server.js b/lib/modules/webserver/server.js similarity index 100% rename from lib/pipeline/server.js rename to lib/modules/webserver/server.js