add webserver start/stop cmd

This commit is contained in:
Iuri Matias 2017-12-17 18:34:41 -05:00
parent 9efd29ec9e
commit 113bb25142
6 changed files with 39 additions and 5 deletions

View File

@ -182,6 +182,14 @@ class Engine {
return cb({name: devServer, status: 'on'}); 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 () { server.start(function () {
}); });
} }

View File

@ -3,6 +3,7 @@ let RunCode = require('../core/runCode.js');
class Console { class Console {
constructor(options) { constructor(options) {
this.events = options.events;
this.plugins = options.plugins; this.plugins = options.plugins;
this.version = options.version; this.version = options.version;
this.contractsConfig = options.contractsConfig; this.contractsConfig = options.contractsConfig;
@ -42,6 +43,12 @@ class Console {
return text.join('\n'); return text.join('\n');
} else if (['quit', 'exit', 'sair', 'sortir'].indexOf(cmd) >= 0) { } else if (['quit', 'exit', 'sair', 'sortir'].indexOf(cmd) >= 0) {
utils.exit(); 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; return false;
} }

View File

@ -6,6 +6,7 @@ let Console = require('./console.js');
class Dashboard { class Dashboard {
constructor(options) { constructor(options) {
this.logger = options.logger; this.logger = options.logger;
this.events = options.events;
this.plugins = options.plugins; this.plugins = options.plugins;
this.version = options.version; this.version = options.version;
this.env = options.env; this.env = options.env;
@ -18,7 +19,12 @@ class Dashboard {
async.waterfall([ async.waterfall([
function startConsole(callback) { 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(); callback();
}, },
function startMonitor(callback) { function startMonitor(callback) {

View File

@ -71,6 +71,7 @@ class Embark {
} }
let dashboard = new Dashboard({ let dashboard = new Dashboard({
events: engine.events,
logger: engine.logger, logger: engine.logger,
plugins: engine.plugins, plugins: engine.plugins,
version: self.version, version: self.version,

View File

@ -1,6 +1,7 @@
let finalhandler = require('finalhandler'); let finalhandler = require('finalhandler');
let http = require('http'); let http = require('http');
let serveStatic = require('serve-static'); let serveStatic = require('serve-static');
require('http-shutdown').extend();
class Server { class Server {
constructor(options) { constructor(options) {
@ -13,14 +14,24 @@ class Server {
start(callback) { start(callback) {
let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']}); 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)); serve(req, res, finalhandler(req, res));
}); }).withShutdown();
this.logger.info("webserver available at " + ("http://" + this.hostname + ":" + this.port).bold.underline.green); this.logger.info("webserver available at " + ("http://" + this.hostname + ":" + this.port).bold.underline.green);
server.listen(this.port, this.hostname); this.server.listen(this.port, this.hostname);
if (callback) {
callback(); callback();
} }
}
stop(callback) {
this.server.shutdown(function() {
if (callback) {
callback();
}
});
}
} }

View File

@ -34,6 +34,7 @@
"follow-redirects": "^1.2.4", "follow-redirects": "^1.2.4",
"fs-extra": "^2.0.0", "fs-extra": "^2.0.0",
"globule": "^1.1.0", "globule": "^1.1.0",
"http-shutdown": "^1.2.0",
"ipfs-api": "17.2.4", "ipfs-api": "17.2.4",
"merge": "^1.2.0", "merge": "^1.2.0",
"mocha": "^2.2.5", "mocha": "^2.2.5",