Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Update application to support https endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rastiqdev committed Jun 22, 2023
1 parent 29ad229 commit 5950955
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Server Port
# HTTP Server Port
PORT=5000

# HTTPS Server Port and certificate (optional)
HTTPSPORT=6001
HTTPS_KEYFILE=
HTTPS_CERTFILE=

# 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
Expand Down
45 changes: 44 additions & 1 deletion bin/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ if (process.env.ANALYTICS_TOKEN) {
analytics = new Analytics(process.env.ANALYTICS_TOKEN)
}

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

if (process.env.HTTPS_KEYFILE !== "undefined") {
try {
key = fs.readFileSync(process.env.HTTPS_KEYFILE)
} 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({
routePrefix: process.env.ROUTE_PREFIX,
repository: process.env.GITHUB_REPO,
Expand Down Expand Up @@ -124,9 +155,21 @@ 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
Expand Down

0 comments on commit 5950955

Please sign in to comment.