2018-08-31 20:01:18 +00:00
|
|
|
const async = require('async');
|
2017-03-29 17:50:05 +00:00
|
|
|
let serveStatic = require('serve-static');
|
2018-07-26 17:13:18 +00:00
|
|
|
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
|
2018-09-19 23:19:37 +00:00
|
|
|
const expressWebSocket = require('express-ws');
|
2018-09-18 13:28:17 +00:00
|
|
|
const express = require('express');
|
|
|
|
const fs = require('../../core/fs');
|
2018-12-04 12:49:24 +00:00
|
|
|
const https = require('https');
|
2018-02-26 00:01:50 +00:00
|
|
|
let path = require('path');
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Server {
|
|
|
|
constructor(options) {
|
2018-10-09 16:44:32 +00:00
|
|
|
this.logger = options.logger;
|
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-09-30 22:56:33 +00:00
|
|
|
this.dist = options.dist || 'dist/';
|
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;
|
2018-09-17 23:47:19 +00:00
|
|
|
this.openBrowser = options.openBrowser;
|
2018-09-28 19:11:26 +00:00
|
|
|
this.logging = false;
|
2018-08-27 20:22:53 +00:00
|
|
|
this.enableCatchAll = options.enableCatchAll;
|
2018-10-09 16:44:32 +00:00
|
|
|
|
2018-12-04 12:49:24 +00:00
|
|
|
this.protocol = options.protocol || 'http';
|
|
|
|
this.certOptions = options.certOptions;
|
|
|
|
|
2018-10-09 16:44:32 +00:00
|
|
|
this.events.once('outputDone', () => {
|
|
|
|
this.logger.info(this._getMessage());
|
|
|
|
});
|
2018-09-28 19:11:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 11:12:39 +00:00
|
|
|
}
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
start(callback) {
|
2018-08-27 20:22:53 +00:00
|
|
|
callback = callback || function() {};
|
2018-09-19 23:19:37 +00:00
|
|
|
const self = this;
|
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-27 20:22:53 +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
|
|
|
}
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2018-09-18 13:28:17 +00: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-12-04 12:49:24 +00:00
|
|
|
this.secureServer = this.protocol === 'https' ? https.createServer(self.certOptions, (req, res) => self.app.handle(req, res)) : null;
|
|
|
|
const expressWs = this.protocol === 'https' ? expressWebSocket(this.app, this.secureServer) : expressWebSocket(this.app);
|
|
|
|
|
2018-09-28 19:11:26 +00:00
|
|
|
// 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-10-02 08:30:20 +00:00
|
|
|
|
2018-10-01 09:10:17 +00:00
|
|
|
this.app.use(main);
|
2018-09-18 13:28:17 +00:00
|
|
|
this.app.use('/coverage', coverage);
|
|
|
|
this.app.use(coverageStyle);
|
2016-08-21 15:02:50 +00:00
|
|
|
|
2018-10-01 08:31:21 +00:00
|
|
|
this.app.use(express.static(path.join(fs.dappPath(this.dist)), {'index': ['index.html', 'index.htm']}));
|
2018-10-31 17:34:00 +00:00
|
|
|
|
|
|
|
this.app.ws('/', () => {});
|
2018-10-03 20:21:59 +00:00
|
|
|
const wss = expressWs.getWss('/');
|
2018-10-03 14:28:00 +00:00
|
|
|
|
2018-10-03 20:21:59 +00:00
|
|
|
self.events.on('outputDone', () => {
|
2018-10-03 20:49:25 +00:00
|
|
|
wss.clients.forEach(function(client) {
|
2018-10-03 20:21:59 +00:00
|
|
|
client.send('outputDone');
|
|
|
|
});
|
2018-10-03 20:46:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
self.events.on('outputError', () => {
|
2018-10-03 20:49:25 +00:00
|
|
|
wss.clients.forEach(function(client) {
|
2018-10-03 20:46:13 +00:00
|
|
|
client.send('outputError');
|
2018-08-02 16:45:59 +00:00
|
|
|
});
|
|
|
|
});
|
2018-08-01 09:19:11 +00:00
|
|
|
|
2018-10-01 08:31:21 +00:00
|
|
|
if (this.enableCatchAll === true) {
|
2018-10-03 20:49:25 +00:00
|
|
|
this.app.get('/*', function(req, res) {
|
2018-10-01 08:31:21 +00:00
|
|
|
self.logger.trace('webserver> GET ' + req.path);
|
|
|
|
res.sendFile(path.join(fs.dappPath(self.dist, 'index.html')));
|
|
|
|
});
|
|
|
|
}
|
2018-07-13 07:50:57 +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) {
|
2018-12-04 12:49:24 +00:00
|
|
|
if (self.protocol === 'https'){
|
|
|
|
self.server = self.secureServer.listen(self.port, self.hostname, () => {
|
|
|
|
self.port = self.secureServer.address().port;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
self.server = self.app.listen(self.port, self.hostname, () => {
|
|
|
|
self.port = self.server.address().port;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
2018-08-31 20:01:18 +00:00
|
|
|
},
|
|
|
|
function openBrowser(next) {
|
2018-09-17 23:47:19 +00:00
|
|
|
if (!self.openBrowser || self.opened) {
|
2018-09-04 13:20:27 +00:00
|
|
|
return next();
|
2018-08-31 20:01:18 +00:00
|
|
|
}
|
2018-09-04 13:20:58 +00:00
|
|
|
self.opened = true;
|
|
|
|
self.events.request('open-browser', next);
|
2018-08-31 16:07:31 +00:00
|
|
|
}
|
2018-10-03 20:49:25 +00:00
|
|
|
], function(err) {
|
2018-09-04 13:20:58 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2018-10-09 16:44:32 +00:00
|
|
|
|
|
|
|
callback(null, self._getMessage(), self.port);
|
2018-09-04 13:20:58 +00:00
|
|
|
});
|
2017-12-17 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:44:32 +00:00
|
|
|
_getMessage() {
|
|
|
|
return __('webserver available at') + ' ' +
|
2018-12-04 12:49:24 +00:00
|
|
|
(this.protocol + '://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green;
|
2018-10-09 16:44:32 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 23:34:41 +00:00
|
|
|
stop(callback) {
|
2018-08-27 20:22:53 +00:00
|
|
|
callback = callback || function () {};
|
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
|
|
|
}
|
2018-09-18 13:28:17 +00:00
|
|
|
this.server.close(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;
|