webserver uses event and waterfall for triggering placeholder build, opening browser

This commit is contained in:
Michael Bradley, Jr 2018-08-31 15:01:18 -05:00
parent 6ee195aba9
commit 52ed0e2856
2 changed files with 41 additions and 14 deletions

View File

@ -24,8 +24,13 @@ class WebServer {
this.events.emit("status", __("Starting Server")); this.events.emit("status", __("Starting Server"));
this.server = new Server({host: this.host, port: this.port}); this.server = new Server({
buildDir: this.buildDir, buildDir: this.buildDir,
events: this.events,
host: this.host,
port: this.port
});
this.testPort(() => { this.testPort(() => {
this.listenToCommands(); this.listenToCommands();
this.registerConsoleCommands(); this.registerConsoleCommands();

View File

@ -1,4 +1,5 @@
let finalhandler = require('finalhandler'); let finalhandler = require('finalhandler');
const async = require('async');
let http = require('http'); let http = require('http');
let serveStatic = require('serve-static'); let serveStatic = require('serve-static');
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host'); const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
@ -8,6 +9,7 @@ require('http-shutdown').extend();
class Server { class Server {
constructor(options) { constructor(options) {
this.buildDir = options.buildDir; this.buildDir = options.buildDir;
this.events = options.events;
this.port = options.port || 8000; this.port = options.port || 8000;
this.hostname = dockerHostSwap(options.host) || defaultHost; this.hostname = dockerHostSwap(options.host) || defaultHost;
this.isFirstStart = true; this.isFirstStart = true;
@ -27,21 +29,41 @@ class Server {
serve(req, res, finalhandler(req, res)); serve(req, res, finalhandler(req, res));
}).withShutdown(); }).withShutdown();
const self = this;
this.server.listen(this.port, this.hostname, () => { async.waterfall([
this.port = this.server.address().port; function createPlaceholderPage(next) {
callback(null, __("webserver available at") + if (self.isFirstStart) {
" " + self.isFirstStart = false;
("http://" + canonicalHost(this.hostname) + return self.events.request('embark-building-placeholder', next);
":" + this.port).bold.underline.green, this.port); }
if (!this.opened) { next();
// fail silently, e.g. in a docker container, by cacthing promise },
// rejection w/ a noop function listen(next) {
opn(`http://${canonicalHost(this.hostname)}:${this.port}`) self.server.listen(self.port, self.hostname, () => {
.catch(function () {}); self.port = self.server.address().port;
this.opened = true; next();
});
},
function openBrowser(next) {
if (!self.opened) {
self.opened = true;
const _next = () => { next(); };
// fail silently, e.g. in a docker container
return opn(
`http://${canonicalHost(self.hostname)}:${self.port}`,
{wait: false}
).then(_next, _next);
}
next();
},
function reportAvailable(next) {
next(null, __("webserver available at") +
" " +
("http://" + canonicalHost(self.hostname) +
":" + self.port).bold.underline.green, self.port);
} }
}); ], callback);
} }
stop(callback) { stop(callback) {