@@ -18,6 +18,37 @@ if (process.env.ANALYTICS_TOKEN) {
18
18
analytics = new Analytics ( process . env . ANALYTICS_TOKEN )
19
19
}
20
20
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
+
21
52
var myNuts = nuts . Nuts ( {
22
53
routePrefix : process . env . ROUTE_PREFIX ,
23
54
repository : process . env . GITHUB_REPO ,
@@ -124,9 +155,21 @@ app.use(function (err, req, res, next) {
124
155
myNuts
125
156
. init ( )
126
157
127
- // Start the HTTP server
158
+ // Start the HTTP and/or HTTPS server
128
159
. then (
129
160
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
+
130
173
var server = app . listen ( process . env . PORT || 5000 , function ( ) {
131
174
var host = server . address ( ) . address
132
175
var port = server . address ( ) . port
0 commit comments