Skip to content

Commit 1f20e3b

Browse files
Added support for https (#26) (#27)
1 parent c284142 commit 1f20e3b

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ const defaultConfiguration = {
4444
maxFieldsSize: mb(20),
4545
maxFileSize: mb(200)
4646
},
47+
https: {
48+
enabled: false,
49+
options: {
50+
key: null,
51+
cert: null
52+
}
53+
},
4754
ws: {
4855
port: null
4956
},

src/core.js

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030

3131
const fs = require('fs-extra');
32+
const http = require('http');
33+
const https = require('https');
3234
const path = require('path');
3335
const morgan = require('morgan');
3436
const express = require('express');
@@ -69,17 +71,21 @@ class Core extends CoreBase {
6971

7072
super(defaultConfiguration, deepmerge(cfg, argvConfig), options);
7173

72-
this.httpServer = null;
7374
this.logger = consola.withTag('Internal');
7475
this.app = express();
75-
this.session = createSession(this.app, this.configuration);
76-
this.ws = createWebsocket(this.app, this.configuration, this.session);
77-
this.wss = this.ws.getWss();
7876

7977
if (!this.configuration.public) {
8078
throw new Error('The public option is required');
8179
}
8280

81+
this.httpServer = this.config('https.enabled')
82+
? https.createServer(this.config('https.options'), this.app)
83+
: http.createServer(this.app);
84+
85+
this.session = createSession(this.app, this.configuration);
86+
this.ws = createWebsocket(this.app, this.configuration, this.session, this.httpServer);
87+
this.wss = this.ws.getWss();
88+
8389
_instance = this;
8490
}
8591

@@ -163,22 +169,27 @@ class Core extends CoreBase {
163169
* Opens HTTP server
164170
*/
165171
listen() {
166-
const wsp = this.configuration.ws.port ? this.configuration.ws.port : this.configuration.port;
167-
const session = path.basename(path.dirname(this.configuration.session.store.module));
168-
const dist = this.configuration.public.replace(process.cwd(), '');
169-
170-
logger.info('Creating HTTP server');
171-
172-
const checkFile = path.join(this.configuration.public, this.configuration.index);
172+
const httpPort = this.config('port');
173+
const wsPort = this.config('ws.port') || httpPort;
174+
const pub = this.config('public');
175+
const session = path.basename(path.dirname(this.config('session.store.module')));
176+
const dist = pub.replace(process.cwd(), '');
177+
const secure = this.config('https.enabled', false);
178+
const proto = prefix => `${prefix}${secure ? 's' : ''}://`;
179+
const host = port => `${this.config('hostname')}:${port}`;
180+
181+
logger.info('Opening server connection');
182+
183+
const checkFile = path.join(pub, this.configuration.index);
173184
if (!fs.existsSync(checkFile)) {
174185
logger.warn('Missing files in "dist/" directory. Did you forget to run "npm run build" ?');
175186
}
176187

177-
this.httpServer = this.app.listen(this.configuration.port, () => {
178-
logger.success(`Server was started on ${this.configuration.hostname}:${this.configuration.port}`);
179-
logger.success(`WebSocket is running on ${this.configuration.hostname}:${wsp}`);
180-
logger.success(`Using ${session} sessions`);
181-
logger.success(`Serving content from ${dist}`);
188+
this.httpServer.listen(httpPort, () => {
189+
logger.success(`Using '${session}' sessions`);
190+
logger.success(`Serving '${dist}'`);
191+
logger.success(`WebSocket listening on ${proto('ws')}${host(wsPort)}`);
192+
logger.success(`Server listening on ${proto('http')}${host(httpPort)}`);
182193
});
183194
}
184195

src/utils/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ module.exports.createSession = (app, configuration) => {
6161
/*
6262
* Create WebSocket server
6363
*/
64-
module.exports.createWebsocket = (app, configuration, session) => express_ws(app, null, {
64+
module.exports.createWebsocket = (app, configuration, session, httpServer) => express_ws(app, httpServer, {
6565
wsOptions: Object.assign({}, configuration.ws, {
6666
verifyClient: (info, done) => {
6767
session(info.req, {}, () => {

0 commit comments

Comments
 (0)