This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
server.js
148 lines (127 loc) · 5.27 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
148
const express = require('express');
const session = require('express-session');
const path = require('path');
const cfenv = require('cfenv');
const bodyParser = require('body-parser');
const DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
const app = express();
// declare service variables
let INVESTMENT_PORFOLIO_BASE_URL, INVESTMENT_PORFOLIO_USERNAME, INVESTMENT_PORFOLIO_PASSWORD;
let DISCOVERY_USERNAME, DISCOVERY_PASSWORD;
let SCENARIO_INSTRUMENTS_URI, SCENARIO_INSTRUMENTS_ACCESS_TOKEN;
let PREDICTIVE_MARKET_SCENARIOS_ACCESS_TOKEN, PREDICTIVE_MARKET_SCENARIOS_URI;
let APPID_TENANTID, APPID_CLIENTID, APPID_SECRET, APPID_OAUTHSERVERURL;
if (process.env.VCAP_SERVICES) {
const env = JSON.parse(process.env.VCAP_SERVICES);
// Find the service
if (env['fss-portfolio-service']) {
INVESTMENT_PORFOLIO_BASE_URL = getHostName(env['fss-portfolio-service'][0].credentials.url);
INVESTMENT_PORFOLIO_USERNAME = env['fss-portfolio-service'][0].credentials.writer.userid;
INVESTMENT_PORFOLIO_PASSWORD = env['fss-portfolio-service'][0].credentials.writer.password;
}
// Find the service
if (env.discovery) {
DISCOVERY_USERNAME = env.discovery[0].credentials.username;
DISCOVERY_PASSWORD = env.discovery[0].credentials.password;
}
// Find the service
if (env['fss-scenario-analytics-service']) {
SCENARIO_INSTRUMENTS_URI = getHostName(env['fss-scenario-analytics-service'][0].credentials.uri);
SCENARIO_INSTRUMENTS_ACCESS_TOKEN = env['fss-scenario-analytics-service'][0].credentials.accessToken;
} else {
console.log('You must bind the Scenario Analytics service to this application');
}
// Find the service
if (env['fss-predictive-scenario-analytics-service']) {
PREDICTIVE_MARKET_SCENARIOS_URI = getHostName(env['fss-predictive-scenario-analytics-service'][0].credentials.uri);
PREDICTIVE_MARKET_SCENARIOS_ACCESS_TOKEN = env['fss-predictive-scenario-analytics-service'][0].credentials.accessToken;
}
// Find the service
if (env.AppID) {
APPID_TENANTID = env.AppID[0].credentials.tenantId;
APPID_CLIENTID = env.AppID[0].credentials.clientId;
APPID_SECRET = env.AppID[0].credentials.secret;
APPID_OAUTHSERVERURL = env.AppID[0].credentials.oauthServerUrl;
}
}
// --Config--------------------
require('dotenv').config();
// --Get the app environment from Cloud Foundry, defaulting to local VCAP--------------------
var vcapLocal = null;
var appEnvOpts = vcapLocal ? {
vcap: vcapLocal
} : {};
var appEnv = cfenv.getAppEnv(appEnvOpts);
if (appEnv.isLocal) {
require('dotenv').load();
}
var port = process.env.VCAP_APP_PORT || appEnv.port;
// --Discovery service setup--------------------
var discoveryUsernameLocal = process.env.DISCOVERY_USERNAME;
var discoveryPasswordLocal = process.env.DISCOVERY_PASSWORD;
var discovery = new DiscoveryV1({
username: discoveryUsernameLocal || DISCOVERY_USERNAME,
password: discoveryPasswordLocal || DISCOVERY_PASSWORD,
version_date: '2017-08-01'
});
// --Setting up the middle ware--------------------
app.use(session({
secret: 'finance-trade-app',
resave: true,
saveUninitialized: true
}));
app.use('/', express.static(`${__dirname}/app`));
app.use('/node_modules', express.static(`${__dirname}/node_modules`));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// routes for user authentication
app.use(require('./routes/auth.js')(app, {
tenantId: APPID_TENANTID || process.env.APPID_TENANTID,
clientId: APPID_CLIENTID || process.env.APPID_CLIENTID,
secret: APPID_SECRET || process.env.APPID_SECRET,
oauthServerUrl: APPID_OAUTHSERVERURL || process.env.APPID_OAUTHSERVERURL,
redirectUri: `${appEnv.url}/auth/callback`
}));
// protect all routes under /api/v1
function checkAuthenticated(req, res, next) {
// if (req.session && req.session.logged) {
if (req.isAuthenticated()) {
next();
} else {
res.sendStatus(401);
}
}
app.use('/api/v1/', checkAuthenticated);
// register API routes
app.use(require('./routes/portfolios.js')({
baseUrl: INVESTMENT_PORFOLIO_BASE_URL || process.env.INVESTMENT_PORFOLIO_BASE_URL,
userid: INVESTMENT_PORFOLIO_USERNAME || process.env.INVESTMENT_PORFOLIO_USERNAME,
password: INVESTMENT_PORFOLIO_PASSWORD || process.env.INVESTMENT_PORFOLIO_PASSWORD,
}));
app.use(require('./routes/news.js')(discovery));
app.use(require('./routes/simulation')({
uri: SCENARIO_INSTRUMENTS_URI || process.env.SIMULATED_INSTRUMENT_ANALYSIS_URI,
accessToken: SCENARIO_INSTRUMENTS_ACCESS_TOKEN ||
process.env.SIMULATED_INSTRUMENT_ANALYSIS_ACCESS_TOKEN
}, {
uri: PREDICTIVE_MARKET_SCENARIOS_URI || process.env.PREDICTIVE_MARKET_SCENARIOS_URI,
accessToken: PREDICTIVE_MARKET_SCENARIOS_ACCESS_TOKEN ||
process.env.PREDICTIVE_MARKET_SCENARIOS_ACCESS_TOKEN
}));
// --All other routes to be sent to home page--------------------
app.get('/*', (req, res) => {
res.sendFile(path.join(`${__dirname}/app/index.html`));
});
function getHostName(url) {
var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (match != null && match.length > 2 && typeof match[2] === 'string' && match[2].length > 0) {
return match[2];
} else {
return null;
}
}
// --launch--------------------
app.listen(port, '0.0.0.0', () => {
// print a message when the server starts listening
console.log(`server running on http://localhost:${port}`);
});