webserver -- canonicalHost, defaultHost, dockerHostSwap

This commit is contained in:
Michael Bradley, Jr 2018-07-26 12:13:18 -05:00
parent d70264adb6
commit 0191ec9d24
2 changed files with 12 additions and 4 deletions

View File

@ -1,3 +1,4 @@
var {canonicalHost} = require('../../utils/host.js');
var utils = require('../../utils/utils.js');
var Server = require('./server.js');
@ -26,7 +27,7 @@ class WebServer {
}
setServiceCheck() {
let url = 'http://' + this.host + ':' + this.port;
let url = 'http://' + canonicalHost(this.host) + ':' + this.port;
this.events.request("services:register", 'Webserver', function (cb) {
utils.checkIsAvailable(url, function (available) {

View File

@ -1,19 +1,23 @@
let finalhandler = require('finalhandler');
let http = require('http');
let serveStatic = require('serve-static');
const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host');
require('http-shutdown').extend();
class Server {
constructor(options) {
this.dist = options.dist || 'dist/';
this.port = options.port || 8000;
this.hostname = options.host || 'localhost';
this.hostname = dockerHostSwap(options.host) || defaultHost;
this.logger = options.logger;
}
start(callback) {
if (this.server && this.server.listening) {
this.logger.warn(__("a webserver is already running at") + " " + ("http://" + this.hostname + ":" + this.port).bold.underline.green);
this.logger.warn(__("a webserver is already running at") +
" " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green);
if (callback) {
callback();
}
@ -25,7 +29,10 @@ class Server {
serve(req, res, finalhandler(req, res));
}).withShutdown();
this.logger.info(__("webserver available at") + " " + ("http://" + this.hostname + ":" + this.port).bold.underline.green);
this.logger.info(__("webserver available at") +
" " +
("http://" + canonicalHost(this.hostname) +
":" + this.port).bold.underline.green);
this.server.listen(this.port, this.hostname);
if (callback) {
callback();