-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathstartserver.js
193 lines (146 loc) · 4.67 KB
/
startserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"use strict";
/**
* Created by johan on 27/04/16.
*
* Should be in a directory 1 level up.
*/
var express = require("express");
var vhost = require("vhost");
var tls = require("tls");
var fs = require("fs");
// pass your own objects/apps or use cody.makeWebApp to generate them
// apps should be an application object or an array of, each application object
// should have
// app [express application]
// host [ string or array of strings ]
// should have at least 1 of the 2 below
// http [ portnumber for the http services ]
// https [ portnumber for the https services ]
// should have -if https port is specified-
// certificate [ filename without extention of the key-pair ]
/*
startserver({ app: testApp, https: 443, host: "localhost", certificate: "mykey" });
or
startserver([{...}, {...}]);
or
host: ["localhost", "www.mysite.com", ...]
Cody production example:
------------------------
var cody = require("cody");
cody.makeWebApp(__dirname, "mysite", function(app) {
console.log("Loaded mysite web app with config:", cody.config);
startServer(app);
});
// for multi site hosting, collect all app's in an array
// and when all set up, pass the array to startServer.
Cody development startup:
-------------------------
var cody = require("cody");
cody.makeWebApp(__dirname, "mysite", "config-devel.json", function(app) {
console.log("loaded mysite");
app.app.listen(app.http, function() {
console.log("mysite listening on " + this.address().port);
});
}
*/
// global express app, wrapping all other apps
var exp;
// global object containing for each domain the certificates
var sites;
// collected ports -> example = { 80: false, 443: true }
var ports = {};
module.exports = function( apps ) {
// make our global express app and an empty site list
exp = express();
sites = [];
if (apps instanceof Array) {
for (let app of apps) {
startApp(app);
}
} else {
startApp(apps);
}
// get at least one listener
var http = false;
var https = false;
for (let port in ports) {
if (!ports[port]) http = true;
if (ports[port]) https = true;
}
if (!http && !https) {
http = true;
ports[3000] = false;
}
console.log("==> ports:", ports);
//////////
// http //
//////////
if (http) {
var httpFactory = require('http');
var httpServer = httpFactory.createServer(exp);
// http ports are "false"
for (let port in ports)
if (! ports[port])
httpServer.listen(port, function () {
console.log("Listening for http on port: " + this.address().port);
});
}
///////////
// https //
///////////
if (https) {
var secureOpts = {
SNICallback: function (domain, cb) {
if (typeof sites[domain] === "undefined") {
cb(new Error("domain not found"), null);
console.log("Error: domain not found: " + domain);
} else {
cb(null, sites[domain]);
}
}
/* left out the default keys:
key: fs.readFileSync('....key').toString(),
cert: fs.readFileSync('....crt').toString()
*/
};
var httpsFactory = require('https');
var httpsServer = httpsFactory.createServer(secureOpts, exp);
// https ports are "true"
for (let port in ports)
if (ports[port])
httpsServer.listen(port, function () {
console.log("Listening https on port: " + this.address().port);
console.log(" with certificates: ", sites);
});
}
};
function startApp( application ) {
// collect extra portnumbers
if (application.http && (typeof ports[application.http] === "undefined")) ports[application.http] = false;
if (application.https && (typeof ports[application.https] === "undefined")) ports[application.https] = true;
var certContext;
// if the application has a certificate, create a tls credential context of it
if ((application.certificate) && (application.certificate.length > 0)) {
certContext = tls.createSecureContext({
key: fs.readFileSync(application.certificate + '.key').toString(),
cert: fs.readFileSync(application.certificate + '.crt').toString()
}).context;
}
// start the application for each (virtual) host
if (application.host instanceof Array) {
for (let host of application.host) {
startHost( host, application.app, certContext );
}
} else {
startHost( application.host, application.app, certContext );
}
}
function startHost( host, app, certContext ) {
console.log("added host " + host);
exp.use(vhost(host, app));
if (certContext) {
// if we have a credential context, add it to our hashmap
console.log("added certificates for " + host);
sites[host] = certContext;
}
}