2017-03-29 17:50:05 +00:00
|
|
|
let finalhandler = require('finalhandler');
|
2018-08-31 20:01:18 +00:00
|
|
|
const async = require('async');
|
2017-03-29 17:50:05 +00:00
|
|
|
let http = require('http');
|
|
|
|
let serveStatic = require('serve-static');
|
2018-07-26 17:13:18 +00:00
|
|
|
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
|
2017-12-17 23:34:41 +00:00
|
|
|
require('http-shutdown').extend();
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Server {
|
|
|
|
constructor(options) {
|
2018-08-31 19:51:18 +00:00
|
|
|
this.buildDir = options.buildDir;
|
2018-08-31 20:01:18 +00:00
|
|
|
this.events = options.events;
|
2017-03-30 11:12:39 +00:00
|
|
|
this.port = options.port || 8000;
|
2018-07-26 17:13:18 +00:00
|
|
|
this.hostname = dockerHostSwap(options.host) || defaultHost;
|
2018-08-31 16:46:41 +00:00
|
|
|
this.isFirstStart = true;
|
|
|
|
this.opened = false;
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
start(callback) {
|
2017-12-18 00:18:35 +00:00
|
|
|
if (this.server && this.server.listening) {
|
2018-08-10 14:09:56 +00:00
|
|
|
let message = __("a webserver is already running at") + " " +
|
2018-08-31 20:02:05 +00:00
|
|
|
("http://" + canonicalHost(this.hostname) +
|
|
|
|
":" + this.port).bold.underline.green;
|
2018-08-10 14:09:56 +00:00
|
|
|
return callback(null, message);
|
2017-12-18 00:18:35 +00:00
|
|
|
}
|
2018-08-31 19:51:18 +00:00
|
|
|
let serve = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']});
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2017-12-17 23:34:41 +00:00
|
|
|
this.server = http.createServer(function onRequest(req, res) {
|
2017-03-30 11:12:39 +00:00
|
|
|
serve(req, res, finalhandler(req, res));
|
2017-12-17 23:34:41 +00:00
|
|
|
}).withShutdown();
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2018-08-31 20:01:18 +00:00
|
|
|
const self = this;
|
2018-08-23 19:11:43 +00:00
|
|
|
|
2018-08-31 20:01:18 +00:00
|
|
|
async.waterfall([
|
|
|
|
function createPlaceholderPage(next) {
|
2018-09-04 13:20:27 +00:00
|
|
|
if (!self.isFirstStart) {
|
|
|
|
return next();
|
2018-08-31 20:01:18 +00:00
|
|
|
}
|
2018-09-04 13:20:27 +00:00
|
|
|
self.isFirstStart = false;
|
|
|
|
self.events.request('build-placeholder', next);
|
2018-08-31 20:01:18 +00:00
|
|
|
},
|
|
|
|
function listen(next) {
|
|
|
|
self.server.listen(self.port, self.hostname, () => {
|
|
|
|
self.port = self.server.address().port;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function openBrowser(next) {
|
2018-09-04 13:20:27 +00:00
|
|
|
if (self.opened) {
|
|
|
|
return next();
|
2018-08-31 20:01:18 +00:00
|
|
|
}
|
|
|
|
function reportAvailable(next) {
|
|
|
|
next(null, __("webserver available at") +
|
|
|
|
" " +
|
|
|
|
("http://" + canonicalHost(self.hostname) +
|
|
|
|
":" + self.port).bold.underline.green, self.port);
|
2018-08-31 16:07:31 +00:00
|
|
|
}
|
2018-08-31 20:01:18 +00:00
|
|
|
], callback);
|
2017-12-17 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stop(callback) {
|
2017-12-18 00:18:35 +00:00
|
|
|
if (!this.server || !this.server.listening) {
|
2018-08-10 14:09:56 +00:00
|
|
|
return callback(null, __("no webserver is currently running"));
|
2017-12-18 00:18:35 +00:00
|
|
|
}
|
2017-12-17 23:34:41 +00:00
|
|
|
this.server.shutdown(function() {
|
2018-08-10 14:09:56 +00:00
|
|
|
callback(null, __("Webserver stopped"));
|
2017-12-17 23:34:41 +00:00
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-21 15:02:50 +00:00
|
|
|
|
|
|
|
module.exports = Server;
|