-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
101 lines (89 loc) · 3.28 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
var logger = require('./utility').logger;
var when = require('when');
var https = require('https');
var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var api = require('./api');
var User = require('./user-mgmt').User;
var config = require('./config');
var useSSL = true; //!config.local;
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
//
// SSL Certificate
//
if (useSSL) {
var credentials;
if (config.local) {
credentials = {
key: fs.readFileSync(__dirname + '/cert/privkey.pem'),
cert: fs.readFileSync(__dirname + '/cert/fullchain.pem')
};
} else {
credentials = {
key: fs.readFileSync(__dirname + '/cert/privkey.pem'),
cert: fs.readFileSync(__dirname + '/cert/fullchain.pem'),
ca: fs.readFileSync(__dirname + '/cert/chain.pem')
};
}
var sslPort = process.env.SSL_PORT || 8443;
}
function initWebClient(app) {
logger.info('Initializing web client');
//
// http://stackoverflow.com/questions/20396900/angularjs-routing-in-expressjs
//
// In order to use AngularJS html5mode along with Express, you must serve "index.html" for all requests to
// leave all routing up to AngularJS. I had this same problem a while back.
// So first, you declare all API endpoint routes, any static file directories (CSS, JS, partials, etc),
// and then serve index.html for all remaining requests.
//
app.use('/styles', express.static(__dirname + '/client/web/styles'));
app.use('/scripts', express.static(__dirname + '/client/web/scripts'));
app.use('/assets', express.static(__dirname + '/client/web/assets'));
app.use('/app', express.static(__dirname + '/client/web/app'));
app.use('/maps', express.static(__dirname + '/client/web/maps'));
// For Let's Encrypt certification
app.use('/.well-known', express.static(__dirname + '/client/web/.well-known', {dotfiles:'allow'}));
// serve index.html for all remaining routes, in order to leave routing up to angular
app.all("/*", function(req, res, next) {
res.sendfile("index.html", { root: __dirname + "/client/web" });
});
}
module.exports = {
init: function() {
logger.info('Stock analytics v0.1');
return when.promise(function(resolve, reject) {
User.init()
.then(function() {
api.init(app);
initWebClient(app);
resolve(null);
})
.catch(function(error) {
logger.error('Fail to init server' + + JSON.stringify(err, null, 2));
reject(error);
});
});
},
getServer: function() {
return app;
},
start: function() {
logger.info('Starting server on port ' + port);
var server = app.listen(port);
if (useSSL) {
logger.info('Starting secure server on port ' + sslPort);
server = https.createServer(credentials, app);
server.listen(sslPort);
}
api.startQuoteServer(server);
},
stop: function() {
api.stopQuoteServer();
logger.info('Server stopped');
}
};