Skip to content

Commit

Permalink
Added support for https (#26) (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud authored Apr 10, 2020
1 parent c284142 commit 1f20e3b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ const defaultConfiguration = {
maxFieldsSize: mb(20),
maxFileSize: mb(200)
},
https: {
enabled: false,
options: {
key: null,
cert: null
}
},
ws: {
port: null
},
Expand Down
43 changes: 27 additions & 16 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/

const fs = require('fs-extra');
const http = require('http');
const https = require('https');
const path = require('path');
const morgan = require('morgan');
const express = require('express');
Expand Down Expand Up @@ -69,17 +71,21 @@ class Core extends CoreBase {

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

this.httpServer = null;
this.logger = consola.withTag('Internal');
this.app = express();
this.session = createSession(this.app, this.configuration);
this.ws = createWebsocket(this.app, this.configuration, this.session);
this.wss = this.ws.getWss();

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

this.httpServer = this.config('https.enabled')
? https.createServer(this.config('https.options'), this.app)
: http.createServer(this.app);

this.session = createSession(this.app, this.configuration);
this.ws = createWebsocket(this.app, this.configuration, this.session, this.httpServer);
this.wss = this.ws.getWss();

_instance = this;
}

Expand Down Expand Up @@ -163,22 +169,27 @@ class Core extends CoreBase {
* Opens HTTP server
*/
listen() {
const wsp = this.configuration.ws.port ? this.configuration.ws.port : this.configuration.port;
const session = path.basename(path.dirname(this.configuration.session.store.module));
const dist = this.configuration.public.replace(process.cwd(), '');

logger.info('Creating HTTP server');

const checkFile = path.join(this.configuration.public, this.configuration.index);
const httpPort = this.config('port');
const wsPort = this.config('ws.port') || httpPort;
const pub = this.config('public');
const session = path.basename(path.dirname(this.config('session.store.module')));
const dist = pub.replace(process.cwd(), '');
const secure = this.config('https.enabled', false);
const proto = prefix => `${prefix}${secure ? 's' : ''}://`;
const host = port => `${this.config('hostname')}:${port}`;

logger.info('Opening server connection');

const checkFile = path.join(pub, this.configuration.index);
if (!fs.existsSync(checkFile)) {
logger.warn('Missing files in "dist/" directory. Did you forget to run "npm run build" ?');
}

this.httpServer = this.app.listen(this.configuration.port, () => {
logger.success(`Server was started on ${this.configuration.hostname}:${this.configuration.port}`);
logger.success(`WebSocket is running on ${this.configuration.hostname}:${wsp}`);
logger.success(`Using ${session} sessions`);
logger.success(`Serving content from ${dist}`);
this.httpServer.listen(httpPort, () => {
logger.success(`Using '${session}' sessions`);
logger.success(`Serving '${dist}'`);
logger.success(`WebSocket listening on ${proto('ws')}${host(wsPort)}`);
logger.success(`Server listening on ${proto('http')}${host(httpPort)}`);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports.createSession = (app, configuration) => {
/*
* Create WebSocket server
*/
module.exports.createWebsocket = (app, configuration, session) => express_ws(app, null, {
module.exports.createWebsocket = (app, configuration, session, httpServer) => express_ws(app, httpServer, {
wsOptions: Object.assign({}, configuration.ws, {
verifyClient: (info, done) => {
session(info.req, {}, () => {
Expand Down

0 comments on commit 1f20e3b

Please sign in to comment.