This repository has been archived by the owner on May 22, 2023. It is now read-only.
/
server.js
147 lines (127 loc) · 4.33 KB
/
server.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
/**
* Spins up two web servers
* - http-server sends the static assets in dist and handles the /config.json endpoint
* - cors-proxy adds the necessary headers for xdomain access to the REST API
*/
var pkg = require('./package.json'),
fs = require('fs'),
morgan = require('morgan'),
http = require('http'),
https = require('https'),
express = require('express'),
finalhandler = require('finalhandler'),
serveFavicon = require('serve-favicon'),
corsAnywhere = require('cors-anywhere'),
COOPR_HOME = process.env.COOPR_HOME;
COOPR_SSL = ('true' == process.env.COOPR_SSL);
COOPR_UI_PORT = parseInt(process.env.COOPR_UI_PORT || 8080, 10),
COOPR_UI_SSL_PORT = parseInt(process.env.COOPR_UI_SSL_PORT || 8443, 10),
COOPR_UI_KEY_FILE = process.env.COOPR_UI_KEY_FILE || COOPR_HOME + 'cert/server.key',
COOPR_UI_CERT_FILE = process.env.COOPR_UI_CERT_FILE || COOPR_HOME + 'cert/server.crt',
COOPR_CORS_PORT = parseInt(process.env.COOPR_CORS_PORT || 8081, 10),
COOPR_SERVER_URI = process.env.COOPR_SERVER_URI || 'http://127.0.0.1:55054',
color = {
hilite: function (v) {
return '\x1B[7m' + v + '\x1B[27m';
},
green: function (v) {
return '\x1B[40m\x1B[32m' + v + '\x1B[39m\x1B[49m';
},
pink: function (v) {
return '\x1B[40m\x1B[35m' + v + '\x1B[39m\x1B[49m';
}
},
sslStarted = false;
morgan.token('cooprcred', function (req, res) {
return color.pink(req.headers['coopr-userid'] + '/' + req.headers['coopr-tenantid']);
});
var httpLabel = color.green('http'),
corsLabel = color.pink('cors'),
httpStaticLogger = morgan(httpLabel + ' :method :url :status'),
httpIndexLogger = morgan(httpLabel + ' :method ' + color.hilite(':url') + ' :status'),
corsLogger = morgan(corsLabel + ' :method :url :cooprcred :status', {
skip: function (req, res) {
return req.method === 'OPTIONS'
}
});
console.log(color.hilite(pkg.name) + ' v' + pkg.version + ' starting up...');
/**
* HTTP server
*/
var app = express(),
sslCredentials = null,
httpServer = http.createServer(app),
httpsServer = null;
if (COOPR_SSL) {
sslCredentials = {
key: fs.readFileSync(COOPR_UI_KEY_FILE, 'utf-8'),
cert: fs.readFileSync(COOPR_UI_CERT_FILE, 'utf-8')
};
httpsServer = https.createServer(sslCredentials, app);
}
try { app.use(serveFavicon(__dirname + '/dist/assets/img/favicon.png')); }
catch(e) { console.error("Favicon missing! Did you run `gulp build`?"); }
// serve the config file
app.get('/config.js', function (req, res) {
var data = JSON.stringify({
// the following will be available in angular via the "MY_CONFIG" injectable
COOPR_SERVER_URI: COOPR_SERVER_URI,
COOPR_CORS_PORT: COOPR_CORS_PORT,
authorization: req.headers.authorization
});
res.header({
'Content-Type': 'text/javascript',
'Cache-Control': 'no-store, must-revalidate'
});
res.send('angular.module("' + pkg.name + '.config", [])' +
'.constant("MY_CONFIG",' + data + ');');
});
// serve static assets
app.use('/assets', [
httpStaticLogger,
express.static(__dirname + '/dist/assets', {
index: false
}),
function(req, res) {
finalhandler(req, res)(false); // 404
}
]);
app.get('/robots.txt', [
httpStaticLogger,
function (req, res) {
res.type('text/plain');
res.send('User-agent: *\nDisallow: /');
}
]);
// any other path, serve index.html
app.all('*', [
httpIndexLogger,
function (req, res) {
res.sendFile(__dirname + '/dist/index.html');
}
]);
if (!COOPR_SSL) {
httpServer.listen(COOPR_UI_PORT, null, null, function () {
console.info(httpLabel + ' listening on port %s', COOPR_UI_PORT);
});
}
if (COOPR_SSL) {
httpsServer.listen(COOPR_UI_SSL_PORT, null, null, function () {
console.info(httpLabel + ' listening on port %s', COOPR_UI_SSL_PORT);
sslStarted = true;
});
}
/**
* CORS proxy
*/
corsAnywhere.createServer({
requireHeader: ['x-requested-with'],
removeHeaders: ['cookie', 'cookie2']
})
.on('request', function (req, res) {
corsLogger(req, res, function noop() {
});
})
.listen(COOPR_CORS_PORT, '0.0.0.0', function () {
console.info(corsLabel + ' listening on port %s', COOPR_CORS_PORT);
});