embark-area-51/lib/modules/webserver/server.js

156 lines
4.6 KiB
JavaScript
Raw Normal View History

const async = require('async');
2017-03-29 17:50:05 +00:00
let serveStatic = require('serve-static');
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');
2017-12-17 23:34:41 +00:00
require('http-shutdown').extend();
var express = require('express');
let path = require('path');
2018-02-26 14:45:46 +00:00
var expressWebSocket = require('express-ws');
2016-08-21 15:02:50 +00:00
2017-03-30 11:12:39 +00:00
class Server {
constructor(options) {
this.logger = options.logger;
this.buildDir = options.buildDir;
this.events = options.events;
2017-03-30 11:12:39 +00:00
this.port = options.port || 8000;
this.hostname = dockerHostSwap(options.host) || defaultHost;
this.isFirstStart = true;
this.opened = false;
this.openBrowser = options.openBrowser;
2018-09-28 19:11:26 +00:00
this.logging = false;
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-09-19 23:19:37 +00:00
const self = this;
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);
}
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-09-27 10:16:55 +00:00
const expressWs = 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-09-18 13:28:17 +00:00
this.app.use(main);
this.app.use('/coverage', coverage);
this.app.use(coverageStyle);
2016-08-21 15:02:50 +00:00
2018-02-26 14:45:46 +00:00
expressWebSocket(this.app);
2018-09-27 10:16:55 +00:00
2018-02-26 14:45:46 +00:00
this.app.ws('/logs', function(ws, req) {
self.events.on("log", function(logLevel, logMsg) {
ws.send(JSON.stringify({msg: logMsg, msg_clear: logMsg.stripColors, logLevel: logLevel}), () => {});
2018-02-26 14:45:46 +00:00
});
2018-03-10 18:12:22 +00:00
});
app.ws('/dashboard', function(ws, req) {
let dashboardState = { contractsState: [], environment: "", status: "", availableServices: [] };
self.events.on('contractsState', (contracts) => {
dashboardState.contractsState = [];
contracts.forEach(function (row) {
dashboardState.contractsState.push({contractName: row[0], address: row[1], status: row[2]});
});
ws.send(dashboardState);
});
self.events.on('status', (status) => {
dashboardState.status = status;
ws.send(dashboardState);
});
self.events.on('servicesState', (servicesState) => {
dashboardState.availableServices = servicesState;
ws.send(dashboardState);
2018-09-19 23:19:37 +00:00
});
2018-10-03 14:28:00 +00:00
});
2018-02-26 14:45:46 +00:00
this.app.ws('/', function(ws, _req) {
self.events.on('outputDone', () => {
if (ws.readyState === WEB_SOCKET_STATE_OPEN) {
return ws.send('outputDone');
}
// if the socket wasn't yet opened, listen for the 'open' event,
// then send the 'outputDone' data
ws.addEventListener('open', _event => {
ws.send('outputDone');
});
});
2018-09-19 23:19:37 +00:00
});
2018-08-23 19:11:43 +00:00
async.waterfall([
function createPlaceholderPage(next) {
if (!self.isFirstStart) {
return next();
}
self.isFirstStart = false;
self.events.request('build-placeholder', next);
},
function listen(next) {
2018-09-18 13:28:17 +00:00
self.server = self.app.listen(self.port, self.hostname, () => {
self.port = self.server.address().port;
next();
});
},
function openBrowser(next) {
if (!self.openBrowser || self.opened) {
return next();
}
2018-09-04 13:20:58 +00:00
self.opened = true;
self.events.request('open-browser', next);
}
2018-09-04 13:20:58 +00:00
], function (err) {
if (err) {
return callback(err);
}
callback(null, self._getMessage(), self.port);
2018-09-04 13:20:58 +00:00
});
2017-12-17 23:34:41 +00:00
}
_getMessage() {
return __('webserver available at') + ' ' +
('http://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green;
}
2017-12-17 23:34:41 +00:00
stop(callback) {
if (!this.server || !this.server.listening) {
2018-08-10 14:09:56 +00:00
return callback(null, __("no webserver is currently running"));
}
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;