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

Commit 5950955

Browse files
committed
Update application to support https endpoints
From GitbookIO#78
1 parent 29ad229 commit 5950955

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

.env.example

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
# Server Port
1+
# HTTP Server Port
22
PORT=5000
33

4+
# HTTPS Server Port and certificate (optional)
5+
HTTPSPORT=6001
6+
HTTPS_KEYFILE=
7+
HTTPS_CERTFILE=
8+
49
# Access token for the GitHub API (requires permissions to access the repository)
510
# If the repository is public you do not need to provide an access token
611
# you can also use GITHUB_USERNAME and GITHUB_PASSWORD

bin/web.js

+44-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,37 @@ if (process.env.ANALYTICS_TOKEN) {
1818
analytics = new Analytics(process.env.ANALYTICS_TOKEN)
1919
}
2020

21+
// Set up for https termination
22+
var key = "",
23+
cert = ""
24+
25+
if (process.env.HTTPS_KEYFILE !== "undefined") {
26+
try {
27+
key = fs.readFileSync(process.env.HTTPS_KEYFILE)
28+
} catch (e) {
29+
if (e.code === "ENOENT") {
30+
console.log("Key file not found!")
31+
} else {
32+
throw e
33+
}
34+
}
35+
}
36+
if (process.env.HTTPS_CERTFILE !== "undefined") {
37+
try {
38+
cert = fs.readFileSync(process.env.HTTPS_CERTFILE)
39+
} catch (e) {
40+
if (e.code === "ENOENT") {
41+
console.log("Certificate file not found!")
42+
} else {
43+
throw e
44+
}
45+
}
46+
}
47+
var https_options = {
48+
key: key,
49+
cert: cert,
50+
}
51+
2152
var myNuts = nuts.Nuts({
2253
routePrefix: process.env.ROUTE_PREFIX,
2354
repository: process.env.GITHUB_REPO,
@@ -124,9 +155,21 @@ app.use(function (err, req, res, next) {
124155
myNuts
125156
.init()
126157

127-
// Start the HTTP server
158+
// Start the HTTP and/or HTTPS server
128159
.then(
129160
function () {
161+
// Enable https endpoint if key and cert are set
162+
if (key != "" && cert != "") {
163+
var https_server = https
164+
.createServer(https_options, app)
165+
.listen(process.env.HTTPSPORT || 5001, function () {
166+
var hosts = https_server.address().address
167+
var ports = https_server.address().port
168+
169+
console.log("Listening at https://%s:%s", hosts, ports)
170+
})
171+
}
172+
130173
var server = app.listen(process.env.PORT || 5000, function () {
131174
var host = server.address().address
132175
var port = server.address().port

0 commit comments

Comments
 (0)