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