96 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-03-30 02:50:05 +09:00
let finalhandler = require('finalhandler');
let http = require('http');
let serveStatic = require('serve-static');
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
2017-12-17 18:34:41 -05:00
require('http-shutdown').extend();
var express = require('express');
let path = require('path');
2018-02-26 09:45:46 -05:00
var expressWebSocket = require('express-ws');
2016-08-21 11:02:50 -04:00
2017-03-30 20:12:39 +09:00
class Server {
constructor(options) {
2018-02-26 17:44:09 -05:00
this.events = options.events;
2017-03-30 20:12:39 +09:00
this.dist = options.dist || 'dist/';
this.port = options.port || 8000;
this.hostname = dockerHostSwap(options.host) || defaultHost;
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-02-26 09:45:46 -05:00
const self = this;
if (this.server && this.server.listening) {
2018-08-10 15:09:56 +01:00
let message = __("a webserver is already running at") + " " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green;
return callback(null, message);
}
2017-03-30 20:12:39 +09:00
let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']});
2016-08-21 11:02:50 -04:00
var app = express();
app.use(serve);
2018-02-26 17:44:09 -05:00
app.use('/embark', serveStatic(path.join(__dirname, 'backend'), {'backend': ['index.html', 'index.htm']}));
2018-02-26 09:45:46 -05:00
expressWebSocket(app);
2018-02-26 17:44:09 -05:00
app.ws('/embark/logs', function(ws, req) {
2018-02-26 09:45:46 -05:00
self.events.on("log", function(logLevel, logMsg) {
ws.send(JSON.stringify({msg: logMsg, msg_clear: logMsg.stripColors, logLevel: logLevel}), () => {});
2018-02-26 09:45:46 -05:00
});
2018-03-10 13:12:22 -05:00
});
app.ws('/embark/dashboard', function(ws, req) {
2018-03-10 13:12:22 -05:00
let dashboardState = { contractsState: [], environment: "", status: "", availableServices: [] };
// TODO: doesn't feel quite right, should be refactored into a shared
// dashboard state
self.events.request('setDashboardState');
2018-03-10 13:12:22 -05:00
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(JSON.stringify(dashboardState));
2018-03-10 13:12:22 -05:00
});
self.events.on('status', (status) => {
dashboardState.status = status;
ws.send(JSON.stringify(dashboardState));
2018-03-10 13:12:22 -05:00
});
self.events.on('servicesState', (servicesState) => {
dashboardState.availableServices = servicesState;
ws.send(JSON.stringify(dashboardState));
2018-02-26 09:45:46 -05:00
});
});
2018-02-26 17:44:09 -05:00
app.get('/embark', function (req, res) {
res.send('Welcome to Embark')
});
2018-02-28 19:00:02 -05:00
app.get('/embark/console', function (req, res) {
self.events.request('console:command', req.query.cmd, res.send.bind(res));
});
app.listen(this.port);
2016-08-21 11:02:50 -04:00
2018-08-10 15:09:56 +01:00
let message = __("webserver available at") +
" " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green;
2017-12-17 18:34:41 -05:00
this.server.listen(this.port, this.hostname);
2018-08-10 15:09:56 +01:00
callback(null, message);
2017-12-17 18:34:41 -05:00
}
stop(callback) {
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 18:34:41 -05:00
this.server.shutdown(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;