webserver uses event and waterfall for triggering placeholder build, opening browser
This commit is contained in:
parent
6ee195aba9
commit
52ed0e2856
|
@ -24,8 +24,13 @@ class WebServer {
|
|||
|
||||
this.events.emit("status", __("Starting Server"));
|
||||
|
||||
this.server = new Server({host: this.host, port: this.port});
|
||||
this.server = new Server({
|
||||
buildDir: this.buildDir,
|
||||
events: this.events,
|
||||
host: this.host,
|
||||
port: this.port
|
||||
});
|
||||
|
||||
this.testPort(() => {
|
||||
this.listenToCommands();
|
||||
this.registerConsoleCommands();
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
let finalhandler = require('finalhandler');
|
||||
const async = require('async');
|
||||
let http = require('http');
|
||||
let serveStatic = require('serve-static');
|
||||
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
|
||||
|
@ -8,6 +9,7 @@ require('http-shutdown').extend();
|
|||
class Server {
|
||||
constructor(options) {
|
||||
this.buildDir = options.buildDir;
|
||||
this.events = options.events;
|
||||
this.port = options.port || 8000;
|
||||
this.hostname = dockerHostSwap(options.host) || defaultHost;
|
||||
this.isFirstStart = true;
|
||||
|
@ -27,21 +29,41 @@ class Server {
|
|||
serve(req, res, finalhandler(req, res));
|
||||
}).withShutdown();
|
||||
|
||||
const self = this;
|
||||
|
||||
this.server.listen(this.port, this.hostname, () => {
|
||||
this.port = this.server.address().port;
|
||||
callback(null, __("webserver available at") +
|
||||
" " +
|
||||
("http://" + canonicalHost(this.hostname) +
|
||||
":" + this.port).bold.underline.green, this.port);
|
||||
if (!this.opened) {
|
||||
// fail silently, e.g. in a docker container, by cacthing promise
|
||||
// rejection w/ a noop
|
||||
opn(`http://${canonicalHost(this.hostname)}:${this.port}`)
|
||||
.catch(function () {});
|
||||
this.opened = true;
|
||||
async.waterfall([
|
||||
function createPlaceholderPage(next) {
|
||||
if (self.isFirstStart) {
|
||||
self.isFirstStart = false;
|
||||
return self.events.request('embark-building-placeholder', next);
|
||||
}
|
||||
next();
|
||||
},
|
||||
function listen(next) {
|
||||
self.server.listen(self.port, self.hostname, () => {
|
||||
self.port = self.server.address().port;
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue