90 lines
2.7 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');
2018-03-13 06:46:58 -04:00
var bodyParser = require('body-parser');
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
this.logger = options.logger;
this.plugins = options.plugins;
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) {
this.logger.warn(__("a webserver is already running at") +
" " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green);
if (callback) {
callback();
}
return;
}
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-03-13 06:46:58 -04:00
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
2018-02-26 09:45:46 -05:00
expressWebSocket(app);
let apiCalls = self.plugins.getPluginsProperty("apiCalls", "apiCalls");
2018-07-12 16:02:16 +03:00
console.dir(apiCalls);
for (let apiCall of apiCalls) {
2018-07-12 16:02:16 +03:00
console.dir("adding " + apiCall.method + " " + apiCall.endpoint);
app[apiCall.method].apply(app, [apiCall.endpoint, apiCall.cb]);
}
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')
});
app.listen(this.port);
2016-08-21 11:02:50 -04:00
2018-07-12 16:02:16 +03:00
this.logger.info(__("webserver available at") + " " + ("http://" + canonicalHost(this.hostname) + ":" + this.port).bold.underline.green);
//this.server.listen(this.port, this.hostname);
//this.server = http.createServer(function onRequest(req, res) {
// serve(req, res, finalhandler(req, res));
//}).withShutdown();
//this.logger.info("webserver available at " + ("http://" + this.hostname + ":" + this.port).bold.underline.green);
//this.server.listen(this.port, this.hostname);
2017-12-17 18:34:41 -05:00
if (callback) {
callback();
}
}
stop(callback) {
if (!this.server || !this.server.listening) {
2018-05-08 17:49:46 -04:00
this.logger.warn(__("no webserver is currently running"));
if (callback) {
callback();
}
return;
}
2017-12-17 18:34:41 -05:00
this.server.shutdown(function() {
if (callback) {
callback();
}
});
2017-03-30 20:12:39 +09:00
}
}
2016-08-21 11:02:50 -04:00
module.exports = Server;