forked from integr8ly/tutorial-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
99 lines (95 loc) · 3.13 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
const express = require('express');
const path = require('path');
const app = express();
const port = process.env.PORT || 5001;
// Dynamic configuration for openshift API calls
app.get('/config.js', (req, res) => {
if (!process.env.OPENSHIFT_HOST) {
console.warn('OPENSHIFT_HOST not set. Using service URLs from env vars');
res.send(`window.OPENSHIFT_CONFIG = {
mockData: {
serviceInstances: [
{
spec: {
clusterServiceClassExternalName: 'Red Hat OpenShift'
},
status: {
dashboardURL:'${process.env.OPENSHIFT_URL}',
conditions: [{ status: 'True' }]
}
},
{
spec: {
clusterServiceClassExternalName: 'Red Hat 3scale API Management Platform'
},
status: {
dashboardURL:'${process.env.OPENSHIFT_URL}',
conditions: [{ status: 'True' }]
}
},
{
spec: {
clusterServiceClassExternalName: 'Red Hat AMQ'
},
status: {
dashboardURL:'${process.env.OPENSHIFT_URL}',
conditions: [{ status: 'True' }]
}
},
{
spec: {
clusterServiceClassExternalName: 'Red Hat Fuse'
},
status: {
dashboardURL:'${process.env.OPENSHIFT_URL}',
conditions: [{ status: 'True' }]
}
},
{
spec: {
clusterServiceClassExternalName: 'Red Hat OpenShift Application Runtimes'
},
status: {
dashboardURL:'${process.env.OPENSHIFT_URL}',
conditions: [{ status: 'True' }]
}
},
{
spec: {
clusterServiceClassExternalName: 'Eclipse Che'
},
status: {
dashboardURL:'${process.env.OPENSHIFT_URL}',
conditions: [{ status: 'True' }]
}
}
]
}
};`);
} else {
let redirectHost;
if (req.headers['x-forwarded-proto'] && req.headers['x-forwarded-host']) {
redirectHost = `${req.headers['x-forwarded-proto']}://${req.headers['x-forwarded-host']}`;
} else {
redirectHost = `https://${req.headers.host}`;
}
res.send(`window.OPENSHIFT_CONFIG = {
clientId: '${process.env.OPENSHIFT_OAUTHCLIENT_ID}',
accessTokenUri: 'https://${process.env.OPENSHIFT_HOST}/oauth/token',
authorizationUri: 'https://${process.env.OPENSHIFT_HOST}/oauth/authorize',
redirectUri: '${redirectHost}/oauth/callback',
scopes: ['user:full'],
masterUri: 'https://${process.env.OPENSHIFT_HOST}',
wssMasterUri: 'wss://${process.env.OPENSHIFT_HOST}'
};`);
}
});
if (process.env.NODE_ENV === 'production') {
// Serve any static files
app.use(express.static(path.join(__dirname, 'build')));
// Handle React routing, return all requests to React app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
}
app.listen(port, () => console.log(`Listening on port ${port}`));