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 = dockerHostSwap(options.host) || defaultHost; } start(callback) { if (this.server && this.server.listening) { let message = __("a webserver is already running at") + " " + ("http://" + canonicalHost(this.hostname) + ":" + this.port).bold.underline.green; return callback(null, message); } let serve = serveStatic(this.dist, {'index': ['index.html', 'index.htm']}); this.server = http.createServer(function onRequest(req, res) { serve(req, res, finalhandler(req, res)); }).withShutdown(); this.server.listen(this.port, this.hostname, () => { this.port = this.server.address().port; callback(null, __("webserver available at") + " " + ("http://" + canonicalHost(this.hostname) + ":" + this.port).bold.underline.green, this.port); }); } stop(callback) { if (!this.server || !this.server.listening) { return callback(null, __("no webserver is currently running")); } this.server.shutdown(function() { callback(null, __("Webserver stopped")); }); } } module.exports = Server;