-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
114 lines (93 loc) · 3.05 KB
/
app.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
/* Load NodeJS Modules */
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const redis = require("redis")
/* Load Local Modules */
const biz = require('./modules/biz');
const db = require('./db/persist');
//Configure express app
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
/* Configure Redis Cache */
console.log("Connecting to Redis...")
var credentials = null;
var vcap = null;
//Check where the Redis instance will come from.
//From CF BackingServiecs, OR a Remote Host OR a local (credentials = null)
if (process.env.VCAP_SERVICES) {
vcap = JSON.parse(process.env.VCAP_SERVICES);
if (vcap.hasOwnProperty('redis-cache')) {
credentials = vcap['redis-cache'][0].credentials;
credentials.host = credentials.hostname
console.log("Redis credentials found in VCAP")
} else {
console.log("No Redis found in VCAP Services")
}
};
if (!credentials) {
//Maybe Redis is on a remote enviroment
console.log("Looking for remote Redis connection details")
if (process.env.REDIS_HOST) {
console.log("trying to connect to Redis on " + process.env.REDIS_HOST)
credentials = {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD,
}
} else {
console.log("No remote Redis details found, will try to connect locally")
}
}
var redisClient = redis.createClient(credentials,);
redisClient.on('error', function (err) {
console.error(err)
console.log('No REDISs. App running without a Cache System');
//console.error(er.stack);
});
redisClient.on('connect', function () {
console.log("Connected to Redis")
biz.SetCache(redisClient)
});
var output = {};
db.Connect(function (error) {
if (error) {
console.error("Can't Connect to Postgres Database");
console.error(error);
}
})
//EndPoint to Retrieve Environment Variables
app.get('/GetEnv', function (req, res) {
// output.sl = slOptions.headers.Cookie || "API Biz Hub"
biz.RetrieveToken(function (cookies) {
output.erp = process.env.ERP
output.token = cookies
output.instance = (process.env.CF_INSTANCE_INDEX * 1) + 1
output.env = process.env.HOME;
//Redis Host
if (process.env.VCAP_SERVICES && process.env.VCAP_SERVICES.hasOwnProperty('redis')) {
output.redis = "Cloud Foundry"
} else {
if (process.env.REDIS_HOST)
output.redis = process.env.REDIS_HOST
};
//PostgreSQL host
if (process.env.VCAP_SERVICES && process.env.VCAP_SERVICES.hasOwnProperty('postgresql')) {
output.pg = "Cloud Foundry"
} else {
if (process.env.PG_HOST)
output.pg = process.env.PG_HOST
};
res.send(output);
})
});
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'views/index.html')));
// Routing
app.use('/erp', require('./routes/erp'));
app.use('/db', require('./routes/db'));
var port = process.env.PORT || 8080
app.listen(port, function () {
console.log('Example app listening on port ' + port);
});