From 113bb25142ca24f6cec9314e0193c785e2ffd57c Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 17 Dec 2017 18:34:41 -0500 Subject: [PATCH] add webserver start/stop cmd --- lib/core/engine.js | 8 ++++++++ lib/dashboard/console.js | 7 +++++++ lib/dashboard/dashboard.js | 8 +++++++- lib/index.js | 1 + lib/pipeline/server.js | 19 +++++++++++++++---- package.json | 1 + 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index 785fc4fd..00b6206e 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -182,6 +182,14 @@ class Engine { return cb({name: devServer, status: 'on'}); }); + self.events.setCommandHandler('start-webserver', function() { + server.start(); + }); + + self.events.setCommandHandler('stop-webserver', function() { + server.stop(); + }); + server.start(function () { }); } diff --git a/lib/dashboard/console.js b/lib/dashboard/console.js index 7ba24d49..48839491 100644 --- a/lib/dashboard/console.js +++ b/lib/dashboard/console.js @@ -3,6 +3,7 @@ let RunCode = require('../core/runCode.js'); class Console { constructor(options) { + this.events = options.events; this.plugins = options.plugins; this.version = options.version; this.contractsConfig = options.contractsConfig; @@ -42,6 +43,12 @@ class Console { return text.join('\n'); } else if (['quit', 'exit', 'sair', 'sortir'].indexOf(cmd) >= 0) { utils.exit(); + } else if (cmd === 'webserver start') { + this.events.request("start-webserver"); + return "starting webserver..."; + } else if (cmd === 'webserver stop') { + this.events.request("stop-webserver"); + return "stopping webserver..."; } return false; } diff --git a/lib/dashboard/dashboard.js b/lib/dashboard/dashboard.js index 82a8c136..bac33282 100644 --- a/lib/dashboard/dashboard.js +++ b/lib/dashboard/dashboard.js @@ -6,6 +6,7 @@ let Console = require('./console.js'); class Dashboard { constructor(options) { this.logger = options.logger; + this.events = options.events; this.plugins = options.plugins; this.version = options.version; this.env = options.env; @@ -18,7 +19,12 @@ class Dashboard { async.waterfall([ function startConsole(callback) { - console = new Console({plugins: self.plugins, version: self.version, contractsConfig: self.contractsConfig}); + console = new Console({ + events: self.events, + plugins: self.plugins, + version: self.version, + contractsConfig: self.contractsConfig + }); callback(); }, function startMonitor(callback) { diff --git a/lib/index.js b/lib/index.js index 935ac916..2788b94a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -71,6 +71,7 @@ class Embark { } let dashboard = new Dashboard({ + events: engine.events, logger: engine.logger, plugins: engine.plugins, version: self.version, diff --git a/lib/pipeline/server.js b/lib/pipeline/server.js index 43d25e57..172cbde9 100644 --- a/lib/pipeline/server.js +++ b/lib/pipeline/server.js @@ -1,6 +1,7 @@ let finalhandler = require('finalhandler'); let http = require('http'); let serveStatic = require('serve-static'); +require('http-shutdown').extend(); class Server { constructor(options) { @@ -13,13 +14,23 @@ class Server { start(callback) { let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']}); - let server = http.createServer(function onRequest(req, res) { + this.server = http.createServer(function onRequest(req, res) { serve(req, res, finalhandler(req, res)); - }); + }).withShutdown(); this.logger.info("webserver available at " + ("http://" + this.hostname + ":" + this.port).bold.underline.green); - server.listen(this.port, this.hostname); - callback(); + this.server.listen(this.port, this.hostname); + if (callback) { + callback(); + } + } + + stop(callback) { + this.server.shutdown(function() { + if (callback) { + callback(); + } + }); } } diff --git a/package.json b/package.json index 8c3abfa4..58ca7030 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "follow-redirects": "^1.2.4", "fs-extra": "^2.0.0", "globule": "^1.1.0", + "http-shutdown": "^1.2.0", "ipfs-api": "17.2.4", "merge": "^1.2.0", "mocha": "^2.2.5",