Make webserver start/stop use callback
This commit is contained in:
parent
0924f2dfd0
commit
0b183c0280
|
@ -17,13 +17,14 @@ class WebServer {
|
|||
this.port = options.port || this.webServerConfig.port;
|
||||
|
||||
this.events.emit("status", __("Starting Server"));
|
||||
this.server = new Server({logger: this.logger, host: this.host, port: this.port});
|
||||
this.server = new Server({host: this.host, port: this.port});
|
||||
|
||||
this.setServiceCheck();
|
||||
this.listenToCommands();
|
||||
this.registerConsoleCommands();
|
||||
|
||||
this.server.start();
|
||||
let self = this;
|
||||
this.server.start((_err, message) => self.logger.info(message));
|
||||
}
|
||||
|
||||
setServiceCheck() {
|
||||
|
@ -43,8 +44,8 @@ class WebServer {
|
|||
}
|
||||
|
||||
listenToCommands() {
|
||||
this.events.setCommandHandler('start-webserver', () => { this.server.start(); });
|
||||
this.events.setCommandHandler('stop-webserver', () => { this.server.stop(); });
|
||||
this.events.setCommandHandler('start-webserver', (callback) => this.server.start(callback));
|
||||
this.events.setCommandHandler('stop-webserver', (callback) => this.server.stop(callback));
|
||||
}
|
||||
|
||||
registerConsoleCommands() {
|
||||
|
@ -52,20 +53,14 @@ class WebServer {
|
|||
self.embark.registerConsoleCommand((cmd, _options) => {
|
||||
return {
|
||||
match: () => cmd === "webserver start",
|
||||
process: (callback) => {
|
||||
self.events.request("start-webserver");
|
||||
callback(null, "OK");
|
||||
}
|
||||
};
|
||||
process: (callback) => self.events.request("start-webserver", callback)
|
||||
}
|
||||
});
|
||||
|
||||
self.embark.registerConsoleCommand((cmd, _options) => {
|
||||
return {
|
||||
match: () => cmd === "webserver stop",
|
||||
process: (callback) => {
|
||||
self.events.request("stop-webserver");
|
||||
callback(null, "OK");
|
||||
}
|
||||
process: (callback) => self.events.request("stop-webserver", callback)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
@ -9,19 +9,14 @@ class Server {
|
|||
this.dist = options.dist || 'dist/';
|
||||
this.port = options.port || 8000;
|
||||
this.hostname = dockerHostSwap(options.host) || defaultHost;
|
||||
this.logger = options.logger;
|
||||
}
|
||||
|
||||
start(callback) {
|
||||
if (this.server && this.server.listening) {
|
||||
this.logger.warn(__("a webserver is already running at") +
|
||||
" " +
|
||||
("http://" + canonicalHost(this.hostname) +
|
||||
":" + this.port).bold.underline.green);
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
let message = __("a webserver is already running at") + " " +
|
||||
("http://" + canonicalHost(this.hostname) +
|
||||
":" + this.port).bold.underline.green;
|
||||
return callback(null, message);
|
||||
}
|
||||
let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']});
|
||||
|
||||
|
@ -29,28 +24,20 @@ class Server {
|
|||
serve(req, res, finalhandler(req, res));
|
||||
}).withShutdown();
|
||||
|
||||
this.logger.info(__("webserver available at") +
|
||||
" " +
|
||||
("http://" + canonicalHost(this.hostname) +
|
||||
":" + this.port).bold.underline.green);
|
||||
let message = __("webserver available at") +
|
||||
" " +
|
||||
("http://" + canonicalHost(this.hostname) +
|
||||
":" + this.port).bold.underline.green;
|
||||
this.server.listen(this.port, this.hostname);
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
callback(null, message);
|
||||
}
|
||||
|
||||
stop(callback) {
|
||||
if (!this.server || !this.server.listening) {
|
||||
this.logger.warn(__("no webserver is currently running"));
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
return;
|
||||
return callback(null, __("no webserver is currently running"));
|
||||
}
|
||||
this.server.shutdown(function() {
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
callback(null, __("Webserver stopped"));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue