mirror of https://github.com/embarklabs/embark.git
Adds https support for dev server
This commit is contained in:
parent
435e1e6471
commit
dffb3b3bb0
|
@ -467,13 +467,25 @@ Config.prototype.loadWebServerConfigFile = function() {
|
|||
"host": defaultHost,
|
||||
"openBrowser": true,
|
||||
"port": 8000,
|
||||
"enableCatchAll": true
|
||||
"enableCatchAll": true,
|
||||
"protocol": "http"
|
||||
};
|
||||
|
||||
let configFilePath = this._getFileOrOject(this.configDir, 'webserver', 'webserver');
|
||||
|
||||
let webServerConfig = this._mergeConfig(configFilePath, configObject, false);
|
||||
|
||||
if (webServerConfig.https){
|
||||
try {
|
||||
webServerConfig.certOptions = {
|
||||
key: fs.readFileSync(webServerConfig.key),
|
||||
cert: fs.readFileSync(webServerConfig.cert)
|
||||
};
|
||||
webServerConfig.protocol = 'https';
|
||||
} catch (e) {
|
||||
webServerConfig.protocol = 'http';
|
||||
}
|
||||
}
|
||||
if (configFilePath === false) {
|
||||
this.webServerConfig = {enabled: false};
|
||||
return;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const ws = new WebSocket(`ws://${location.hostname}:${location.port}`);
|
||||
const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${location.port}`);
|
||||
ws.addEventListener('message', (evt) => {
|
||||
if(evt.data === 'outputDone') {
|
||||
location.reload(true);
|
||||
|
|
|
@ -40,7 +40,9 @@ class WebServer {
|
|||
host: this.host,
|
||||
port: this.port,
|
||||
plugins: this.plugins,
|
||||
openBrowser: this.webServerConfig.openBrowser
|
||||
openBrowser: this.webServerConfig.openBrowser,
|
||||
protocol: this.webServerConfig.protocol,
|
||||
certOptions : this.webServerConfig.certOptions
|
||||
});
|
||||
|
||||
this.listenToCommands();
|
||||
|
@ -54,6 +56,8 @@ class WebServer {
|
|||
this.port = this.webServerConfig.port;
|
||||
this.server.host = this.host;
|
||||
this.server.port = this.port;
|
||||
this.server.protocol = this.webServerConfig.protocol;
|
||||
this.server.certOptions = this.webServerConfig.certOptions;
|
||||
|
||||
this.testPort(() => {
|
||||
this.events.request('processes:stop', 'webserver', _err => {
|
||||
|
@ -81,7 +85,7 @@ class WebServer {
|
|||
this.server.port = 0;
|
||||
return done();
|
||||
}
|
||||
utils.pingEndpoint(this.host, this.port, 'http', 'http', '', (err) => {
|
||||
utils.pingEndpoint(this.host, this.port, this.protocol, this.protocol, '', (err) => {
|
||||
if (err) { // Port is ok
|
||||
return done();
|
||||
}
|
||||
|
@ -96,7 +100,7 @@ class WebServer {
|
|||
const self = this;
|
||||
|
||||
this.events.request("services:register", 'Webserver', function (cb) {
|
||||
let url = 'http://' + canonicalHost(self.host) + ':' + self.port;
|
||||
let url = this.protocol + '://' + canonicalHost(self.host) + ':' + self.port;
|
||||
utils.checkIsAvailable(url, function (available) {
|
||||
let devServer = __('Webserver') + ' (' + url + ')';
|
||||
let serverStatus = (available ? 'on' : 'off');
|
||||
|
@ -165,7 +169,7 @@ class WebServer {
|
|||
openBrowser(cb) {
|
||||
const _cb = () => { cb(); };
|
||||
return opn(
|
||||
`http://${canonicalHost(this.server.hostname)}:${this.server.port}`,
|
||||
`${this.protocol}://${canonicalHost(this.server.hostname)}:${this.server.port}`,
|
||||
{wait: false}
|
||||
).then(_cb, _cb); // fail silently, e.g. in a docker container
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ const {canonicalHost, defaultHost, dockerHostSwap} = require('../../utils/host')
|
|||
const expressWebSocket = require('express-ws');
|
||||
const express = require('express');
|
||||
const fs = require('../../core/fs');
|
||||
const https = require('https');
|
||||
var cors = require('cors');
|
||||
let path = require('path');
|
||||
var bodyParser = require('body-parser');
|
||||
|
@ -24,6 +25,9 @@ class Server {
|
|||
this.plugins = options.plugins;
|
||||
this.enableCatchAll = options.enableCatchAll;
|
||||
|
||||
this.protocol = options.protocol || 'http';
|
||||
this.certOptions = options.certOptions;
|
||||
|
||||
this.events.once('outputDone', () => {
|
||||
this.logger.info(this._getMessage());
|
||||
});
|
||||
|
@ -54,7 +58,9 @@ class Server {
|
|||
const main = serveStatic(this.buildDir, {'index': ['index.html', 'index.htm']});
|
||||
|
||||
this.app = express();
|
||||
const expressWs = expressWebSocket(this.app);
|
||||
this.secureServer = this.protocol === 'https' ? https.createServer(self.certOptions, (req, res) => self.app.handle(req, res)) : null;
|
||||
const expressWs = this.protocol === 'https' ? expressWebSocket(this.app, this.secureServer) : expressWebSocket(this.app);
|
||||
|
||||
// Assign Logging Function
|
||||
this.app.use(function(req, res, next) {
|
||||
if (self.logging) {
|
||||
|
@ -136,10 +142,18 @@ class Server {
|
|||
self.events.request('build-placeholder', next);
|
||||
},
|
||||
function listen(next) {
|
||||
self.server = self.app.listen(self.port, self.hostname, () => {
|
||||
self.port = self.server.address().port;
|
||||
next();
|
||||
});
|
||||
if (self.protocol === 'https'){
|
||||
self.server = self.secureServer.listen(self.port, self.hostname, () => {
|
||||
self.port = self.secureServer.address().port;
|
||||
next();
|
||||
});
|
||||
}
|
||||
else{
|
||||
self.server = self.app.listen(self.port, self.hostname, () => {
|
||||
self.port = self.server.address().port;
|
||||
next();
|
||||
});
|
||||
}
|
||||
},
|
||||
function openBrowser(next) {
|
||||
if (!self.openBrowser || self.opened) {
|
||||
|
@ -159,7 +173,7 @@ class Server {
|
|||
|
||||
_getMessage() {
|
||||
return __('webserver available at') + ' ' +
|
||||
('http://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green;
|
||||
(this.protocol + '://' + canonicalHost(this.hostname) + ':' + this.port).bold.underline.green;
|
||||
}
|
||||
|
||||
applyAPIFunction(cb, req, res) {
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
<h1 class="loading-msg"><%- buildingMsg %></h1>
|
||||
</body>
|
||||
<script>
|
||||
const ws = new WebSocket(`ws://${location.hostname}:${location.port}`);
|
||||
const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.hostname}:${location.port}`);
|
||||
ws.addEventListener('message', (evt) => {
|
||||
if(evt.data === 'outputDone') {
|
||||
location.reload(true);
|
||||
|
|
|
@ -31,11 +31,20 @@ function recursiveMerge(target, source) {
|
|||
}
|
||||
|
||||
function checkIsAvailable(url, callback) {
|
||||
http.get(url, function (_res) {
|
||||
callback(true);
|
||||
}).on('error', function (_res) {
|
||||
callback(false);
|
||||
});
|
||||
const protocol = url.split(':')[0];
|
||||
if (protocol === 'http') {
|
||||
http.get(url, function (_res) {
|
||||
callback(true);
|
||||
}).on('error', function (_res) {
|
||||
callback(false);
|
||||
});
|
||||
} else if (protocol === 'https') {
|
||||
https.get(url, function (_res) {
|
||||
callback(true);
|
||||
}).on('error', function (_res) {
|
||||
callback(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function httpGetRequest(httpObj, url, callback) {
|
||||
|
|
Loading…
Reference in New Issue