2018-08-31 15:01:18 -05:00
|
|
|
const async = require('async');
|
2017-03-30 02:50:05 +09:00
|
|
|
let serveStatic = require('serve-static');
|
2018-07-26 12:13:18 -05:00
|
|
|
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
|
2018-09-19 19:19:37 -04:00
|
|
|
const expressWebSocket = require('express-ws');
|
2018-09-18 14:28:17 +01:00
|
|
|
const express = require('express');
|
|
|
|
const fs = require('../../core/fs');
|
2017-12-17 18:34:41 -05:00
|
|
|
require('http-shutdown').extend();
|
2016-08-21 11:02:50 -04:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
class Server {
|
|
|
|
constructor(options) {
|
2018-10-09 12:44:32 -04:00
|
|
|
this.logger = options.logger;
|
2018-08-31 14:51:18 -05:00
|
|
|
this.buildDir = options.buildDir;
|
2018-08-31 15:01:18 -05:00
|
|
|
this.events = options.events;
|
2017-03-30 20:12:39 +09:00
|
|
|
this.port = options.port || 8000;
|
2018-07-26 12:13:18 -05:00
|
|
|
this.hostname = dockerHostSwap(options.host) || defaultHost;
|
2018-08-31 11:46:41 -05:00
|
|
|
this.isFirstStart = true;
|
|
|
|
this.opened = false;
|
2018-09-17 18:47:19 -05:00
|
|
|
this.openBrowser = options.openBrowser;
|
2018-09-29 00:41:26 +05:30
|
|
|
this.logging = false;
|
2018-10-09 12:44:32 -04:00
|
|
|
|
|
|
|
this.events.once('outputDone', () => {
|
|
|
|
this.logger.info(this._getMessage());
|
|
|
|
});
|
2018-09-29 00:41:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
enableLogging(callback) {
|
|
|
|
this.logging = true;
|
|
|
|
return callback(null, __("Enabled Webserver Logs"));
|
|
|
|
}
|
|
|
|
|
|
|
|
disableLogging(callback) {
|
|
|
|
this.logging = false;
|
|
|
|
return callback(null, __("Disabled Webserver Logs"));
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
2016-08-21 11:02:50 -04:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
start(callback) {
|
2018-09-19 19:19:37 -04:00
|
|
|
const self = this;
|
2017-12-17 19:18:35 -05:00
|
|
|
if (this.server && this.server.listening) {
|
2018-08-10 15:09:56 +01:00
|
|
|
let message = __("a webserver is already running at") + " " +
|
2018-08-31 15:02:05 -05:00
|
|
|
("http://" + canonicalHost(this.hostname) +
|
|
|
|
":" + this.port).bold.underline.green;
|
2018-08-10 15:09:56 +01:00
|
|
|
return callback(null, message);
|
2017-12-17 19:18:35 -05:00
|
|
|
}
|
2016-08-21 11:02:50 -04:00
|
|
|
|
2018-09-18 14:28:17 +01:00
|
|
|
const coverage = serveStatic(fs.dappPath('coverage/__root__/'), {'index': ['index.html', 'index.htm']});
|
|
|
|
const coverageStyle = serveStatic(fs.dappPath('coverage/'));
|
|
|
|
const main = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']});
|
|
|
|
|
|
|
|
this.app = express();
|
2018-09-27 11:16:55 +01:00
|
|
|
const expressWs = expressWebSocket(this.app);
|
2018-10-09 12:44:32 -04:00
|
|
|
|
2018-09-29 00:41:26 +05:30
|
|
|
// Assign Logging Function
|
|
|
|
this.app.use(function(req, res, next) {
|
|
|
|
if (self.logging) {
|
|
|
|
if (!req.headers.upgrade) {
|
|
|
|
console.log('Webserver> ' + req.method + " " + req.originalUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2018-09-18 14:28:17 +01:00
|
|
|
this.app.use(main);
|
|
|
|
this.app.use('/coverage', coverage);
|
|
|
|
this.app.use(coverageStyle);
|
2016-08-21 11:02:50 -04:00
|
|
|
|
2018-09-27 11:16:55 +01:00
|
|
|
this.app.ws('/', (_ws, _req) => {});
|
|
|
|
const wss = expressWs.getWss('/');
|
|
|
|
|
|
|
|
self.events.on('outputDone', () => {
|
|
|
|
wss.clients.forEach(function (client) {
|
|
|
|
client.send('outputDone');
|
2018-09-19 19:19:37 -04:00
|
|
|
});
|
2018-10-03 15:28:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
self.events.on('outputError', () => {
|
|
|
|
wss.clients.forEach(function (client) {
|
|
|
|
client.send('outputError');
|
2018-10-01 22:58:42 +02:00
|
|
|
});
|
2018-09-19 19:19:37 -04:00
|
|
|
});
|
2018-08-23 15:11:43 -04:00
|
|
|
|
2018-08-31 15:01:18 -05:00
|
|
|
async.waterfall([
|
|
|
|
function createPlaceholderPage(next) {
|
2018-09-04 08:20:27 -05:00
|
|
|
if (!self.isFirstStart) {
|
|
|
|
return next();
|
2018-08-31 15:01:18 -05:00
|
|
|
}
|
2018-09-04 08:20:27 -05:00
|
|
|
self.isFirstStart = false;
|
|
|
|
self.events.request('build-placeholder', next);
|
2018-08-31 15:01:18 -05:00
|
|
|
},
|
|
|
|
function listen(next) {
|
2018-09-18 14:28:17 +01:00
|
|
|
self.server = self.app.listen(self.port, self.hostname, () => {
|
2018-08-31 15:01:18 -05:00
|
|
|
self.port = self.server.address().port;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function openBrowser(next) {
|
2018-09-17 18:47:19 -05:00
|
|
|
if (!self.openBrowser || self.opened) {
|
2018-09-04 08:20:27 -05:00
|
|
|
return next();
|
2018-08-31 15:01:18 -05:00
|
|
|
}
|
2018-09-04 08:20:58 -05:00
|
|
|
self.opened = true;
|
|
|
|
self.events.request('open-browser', next);
|
2018-08-31 11:07:31 -05:00
|
|
|
}
|
2018-09-04 08:20:58 -05:00
|
|
|
], function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-10-09 12:44:32 -04:00
|
|
|
|
|
|
|
callback(null, self._getMessage(), self.port);
|
2018-09-04 08:20:58 -05:00
|
|
|
});
|
2017-12-17 18:34:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-09 12:44:32 -04:00
|
|
|
_getMessage() {
|
|
|
|
return __('webserver available at') + ' ' +
|
|
|
|
('http://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green;
|
|
|
|
}
|
|
|
|
|
2017-12-17 18:34:41 -05:00
|
|
|
stop(callback) {
|
2017-12-17 19:18:35 -05:00
|
|
|
if (!this.server || !this.server.listening) {
|
2018-08-10 15:09:56 +01:00
|
|
|
return callback(null, __("no webserver is currently running"));
|
2017-12-17 19:18:35 -05:00
|
|
|
}
|
2018-09-18 14:28:17 +01:00
|
|
|
this.server.close(function() {
|
2018-08-10 15:09:56 +01:00
|
|
|
callback(null, __("Webserver stopped"));
|
2017-12-17 18:34:41 -05:00
|
|
|
});
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
|
|
|
}
|
2016-08-21 11:02:50 -04:00
|
|
|
|
|
|
|
module.exports = Server;
|