Skip to content

Commit

Permalink
Added HTTPS support
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaCrafter committed Oct 26, 2018
1 parent 828db17 commit 127d8ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
| `session.secret` | Required | Private string for cookie |
| `session.ttl` | `36` | Cookie TTL in hours |
| | | |
| `ssl` | Optional | Enables HTTPS mode if filled |
| `ssl.key` | Required | Local path to private key |
| `ssl.cert` | Required | Local path to certificate file |
| | | |
| `devMode` | `false` | If `true` controllers and models refreshing on every request |
| `port` | `8081` | API listing port |
| `ws_timeout` | `60` | WebSocket request waiting timeout in seconds |
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,29 @@ const DB = require('./DB');
});
});

// Creating HTTPS server, if ssl enabled
if(config.ssl) {
const https = require('https');
var server = https.createServer({
key: fs.readFileSync(ROOT + '/' + config.ssl.key, 'utf8'),
cert: fs.readFileSync(ROOT + '/' + config.ssl.cert, 'utf8')
}, app);
}

// Dispatching requests
const dispatch = require('./dispatch');
dispatch.db = MainDB;
require('express-ws')(app);
require('express-ws')(app, config.ssl?server:undefined);
app.ws('/ws', (ws, req) => dispatch.ws(req, ws));
app.all('*', (req, res) => dispatch.http(req, res));

// Listening port
config.port = config.port || 8081;
app.listen(config.port, () => {
console.log('[Core] Started at port ' + config.port);
});
const listenArgs = [
config.port,
() => console.log('[Core] Started' + (config.ssl?' with SSL':'') + ' at port ' + config.port)
];

if(config.ssl) server.listen(...listenArgs);
else app.listen(...listenArgs);
})();
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dc-api-core",
"version": "0.1.6",
"version": "0.1.7",
"author": "DimaCrafter",
"homepage": "https://github.com/DimaCrafter/dc-api-core",
"bugs": "https://github.com/DimaCrafter/dc-api-core/issues",
Expand All @@ -10,7 +10,7 @@
"express": "^4.16.4",
"express-session": "^1.15.6",
"express-ws": "^4.0.0",
"mongoose": "^5.3.4",
"mongoose": "^5.3.7",
"mongoose-auto-increment": "^5.0.1"
}
}

0 comments on commit 127d8ed

Please sign in to comment.