2018-08-31 19:59:54 +00:00
|
|
|
const fs = require('../../core/fs.js');
|
2018-07-26 17:13:18 +00:00
|
|
|
var {canonicalHost} = require('../../utils/host.js');
|
2017-12-18 14:37:16 +00:00
|
|
|
var utils = require('../../utils/utils.js');
|
|
|
|
var Server = require('./server.js');
|
|
|
|
|
2018-08-31 19:59:54 +00:00
|
|
|
require('ejs');
|
|
|
|
const Templates = {
|
|
|
|
embark_building_placeholder: require('./templates/embark-building-placeholder.html.ejs')
|
|
|
|
};
|
2017-12-18 14:37:16 +00:00
|
|
|
|
2018-08-31 19:59:54 +00:00
|
|
|
class WebServer {
|
2017-12-18 14:37:16 +00:00
|
|
|
constructor(embark, options) {
|
2017-12-29 23:29:56 +00:00
|
|
|
this.embark = embark;
|
2017-12-18 14:37:16 +00:00
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
2018-08-31 19:51:18 +00:00
|
|
|
this.buildDir = options.buildDir;
|
2017-12-19 14:50:29 +00:00
|
|
|
this.webServerConfig = embark.config.webServerConfig;
|
|
|
|
if (!this.webServerConfig.enabled) {
|
2017-12-18 14:37:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-19 14:50:29 +00:00
|
|
|
this.host = options.host || this.webServerConfig.host;
|
|
|
|
this.port = options.port || this.webServerConfig.port;
|
2017-12-18 14:37:16 +00:00
|
|
|
|
2018-05-08 21:49:46 +00:00
|
|
|
this.events.emit("status", __("Starting Server"));
|
2017-12-19 14:50:29 +00:00
|
|
|
|
2018-08-31 20:01:18 +00:00
|
|
|
this.server = new Server({
|
2018-08-31 19:51:18 +00:00
|
|
|
buildDir: this.buildDir,
|
2018-08-31 20:01:18 +00:00
|
|
|
events: this.events,
|
|
|
|
host: this.host,
|
|
|
|
port: this.port
|
|
|
|
});
|
|
|
|
|
2018-08-23 18:28:44 +00:00
|
|
|
this.testPort(() => {
|
2018-08-23 18:14:10 +00:00
|
|
|
this.listenToCommands();
|
|
|
|
this.registerConsoleCommands();
|
2018-08-23 19:11:43 +00:00
|
|
|
this.server.start((_err, message, port) => {
|
|
|
|
this.logger.info(message);
|
|
|
|
this.port = port;
|
|
|
|
this.setServiceCheck();
|
|
|
|
});
|
2018-08-23 18:14:10 +00:00
|
|
|
});
|
2017-12-19 14:50:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 18:28:44 +00:00
|
|
|
testPort(done) {
|
|
|
|
utils.pingEndpoint(this.host, this.port, 'http', 'http', '', (err) => {
|
|
|
|
if (err) { // Port is ok
|
|
|
|
return done();
|
|
|
|
}
|
2018-08-23 19:11:43 +00:00
|
|
|
this.logger.warn(__('Webserver already running on port %s. Assigning an available port', this.port));
|
|
|
|
this.port = 0;
|
|
|
|
this.server.port = 0;
|
|
|
|
done();
|
2018-08-23 18:28:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-19 14:50:29 +00:00
|
|
|
setServiceCheck() {
|
2018-08-23 19:11:43 +00:00
|
|
|
const self = this;
|
2017-12-18 14:37:16 +00:00
|
|
|
|
2018-06-01 15:58:11 +00:00
|
|
|
this.events.request("services:register", 'Webserver', function (cb) {
|
2018-08-23 19:11:43 +00:00
|
|
|
let url = 'http://' + canonicalHost(self.host) + ':' + self.port;
|
2017-12-19 14:50:29 +00:00
|
|
|
utils.checkIsAvailable(url, function (available) {
|
2018-05-08 21:49:46 +00:00
|
|
|
let devServer = __('Webserver') + ' (' + url + ')';
|
2017-12-19 14:50:29 +00:00
|
|
|
let serverStatus = (available ? 'on' : 'off');
|
|
|
|
return cb({name: devServer, status: serverStatus});
|
2017-12-18 14:37:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-12-19 14:50:29 +00:00
|
|
|
this.events.on('check:wentOffline:Webserver', () => {
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.info(__("Webserver is offline"));
|
2017-12-18 14:37:16 +00:00
|
|
|
});
|
2017-12-19 14:50:29 +00:00
|
|
|
}
|
2017-12-18 14:37:16 +00:00
|
|
|
|
2017-12-19 14:50:29 +00:00
|
|
|
listenToCommands() {
|
2018-08-31 19:59:54 +00:00
|
|
|
this.events.setCommandHandler('embark-building-placeholder', (cb) => this.buildPlaceholderPage(cb));
|
2018-08-10 14:09:56 +00:00
|
|
|
this.events.setCommandHandler('start-webserver', (callback) => this.server.start(callback));
|
|
|
|
this.events.setCommandHandler('stop-webserver', (callback) => this.server.stop(callback));
|
2017-12-18 14:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-29 23:29:56 +00:00
|
|
|
registerConsoleCommands() {
|
|
|
|
const self = this;
|
2017-12-29 23:33:11 +00:00
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
2018-08-08 12:42:45 +00:00
|
|
|
return {
|
|
|
|
match: () => cmd === "webserver start",
|
2018-08-10 14:09:56 +00:00
|
|
|
process: (callback) => self.events.request("start-webserver", callback)
|
2018-08-10 14:54:21 +00:00
|
|
|
};
|
2017-12-29 23:29:56 +00:00
|
|
|
});
|
|
|
|
|
2018-08-08 12:42:45 +00:00
|
|
|
self.embark.registerConsoleCommand((cmd, _options) => {
|
|
|
|
return {
|
|
|
|
match: () => cmd === "webserver stop",
|
2018-08-10 14:09:56 +00:00
|
|
|
process: (callback) => self.events.request("stop-webserver", callback)
|
2018-08-08 12:42:45 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
2018-08-31 19:59:54 +00:00
|
|
|
|
|
|
|
buildPlaceholderPage(cb) {
|
|
|
|
let html = Templates.embark_building_placeholder({buildingMsg: __('Embark is building, please wait...')});
|
|
|
|
fs.mkdirpSync(this.buildDir); // create buildDir/ folder if not already exists
|
|
|
|
fs.writeFile(utils.joinPath(this.buildDir, 'index.html'), html, cb);
|
|
|
|
}
|
2017-12-18 14:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = WebServer;
|