Skip to content

Update application to support https endpoints #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion bin/web.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,9 @@ var uuid = require('uuid');
var basicAuth = require('basic-auth');
var Analytics = require('analytics-node');
var nuts = require('../');
var fs = require('fs');
var https = require('https');


var app = express();

@@ -17,6 +20,36 @@ if (process.env.ANALYTICS_TOKEN) {
analytics = new Analytics(process.env.ANALYTICS_TOKEN);
}

// Set up for https termination
var key = "", cert = ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var key, cert

should be enough


if (process.env.HTTPS_KEYFILE !== 'undefined') {
try {
key = fs.readFileSync(process.env.HTTPS_KEYFILE);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would just throw here in any case, if the env var is set and there's an error reading the file, fail hard

} catch (e) {
if (e.code === 'ENOENT') {
console.log('Key file not found!');
} else {
throw e;
}
}
}
if (process.env.HTTPS_CERTFILE !== 'undefined') {
try {
cert = fs.readFileSync(process.env.HTTPS_CERTFILE);
} catch (e) {
if (e.code === 'ENOENT') {
console.log('Certificate file not found!');
} else {
throw e;
}
}
}
var https_options = {
key: key,
cert: cert
};

var myNuts = nuts.Nuts({
repository: process.env.GITHUB_REPO,
token: process.env.GITHUB_TOKEN,
@@ -118,8 +151,19 @@ app.use(function(err, req, res, next) {

myNuts.init()

// Start the HTTP server
// Start the HTTP and/or HTTPS server
.then(function() {

// Enable https endpoint if key and cert are set
if(key != "" && cert != "") {
var https_server = https.createServer(https_options, app).listen(process.env.HTTPSPORT || 5001, function () {
var hosts = https_server.address().address;
var ports = https_server.address().port;

console.log('Listening at https://%s:%s', hosts, ports);
});
}

var server = app.listen(process.env.PORT || 5000, function () {
var host = server.address().address;
var port = server.address().port;
7 changes: 6 additions & 1 deletion docs/deploy.md
Original file line number Diff line number Diff line change
@@ -27,9 +27,14 @@ $ npm install
This service requires to be configured using environment variables:

```
# Set the port for the service
# Set the port for the http service
$ export PORT=6000

# Set the port for the https service (optional)
$ export HTTPSPORT=6001
$ export HTTPS_KEYFILE=<Full Path to Private Key File>
$ export HTTPS_CERTFILE=<Full Path to Public Certificate File>

# Access token for the GitHub API (requires permissions to access the repository)
# If the repository is public you do not need to provide an access token
# you can also use GITHUB_USERNAME and GITHUB_PASSWORD