-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathserver.js
More file actions
129 lines (114 loc) · 3.23 KB
/
server.js
File metadata and controls
129 lines (114 loc) · 3.23 KB
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
const path = require('path')
var express = require('express')
var expressStaticGzip = require('express-static-gzip')
const helmet = require('helmet')
const timeout = require('connect-timeout')
const morgan = require('morgan')
const httpProxy = require('http-proxy')
const healthcheck = require('express-healthcheck')
const CONSTANTS = {
PORTAL_PORT: parseInt(process.env.PORTAL_PORT || '3002', 10),
LA_HOST_PROXY: process.env.LA_HOST_PROXY || 'http://localhost',
WEB_HOST_PROXY: process.env.WEB_HOST_PROXY || 'http://localhost:3007',
FRAME_ANCESTORS: process.env.FRAME_ANCESTORS || "'self'",
}
var app = express()
var proxy = httpProxy.createProxyServer({
timeout: 10000,
})
app.use(timeout('100s'))
app.use('/healthcheck', healthcheck({
healthy() {
return { everything: 'is ok' }
},
}))
app.engine('html', require('ejs').renderFile);
// Add required helmet configurations
app.use(
helmet({
frameguard: {
action: 'sameorigin',
},
noCache: false,
hidePoweredBy: true,
ieNoOpen: true,
dnsPrefetchControl: {
allow: true,
},
noSniff: true,
contentSecurityPolicy: {
directives: {
frameAncestors: CONSTANTS.FRAME_ANCESTORS.split(','),
},
},
}),
)
app.use('/LA', proxyCreator(express.Router(), CONSTANTS.LA_HOST_PROXY))
app.use(morgan('combined'))
app.use(haltOnTimedOut)
app.use('/ScormCoursePlayer', proxyCreator(express.Router(), 'http://localhost/ScormCoursePlayer'))
app.disable('etag')
app.set('etag', false)
serveAssets('')
serveAssets('/ar')
serveAssets('/de')
serveAssets('/es')
serveAssets('/fr')
serveAssets('/fr-ca')
serveAssets('/nl')
serveAssets('/zh-CN')
serveAssets('/ja')
function serveAssets(hostPath) {
app.use(
`${hostPath}/assets`,
// proxyCreator(express.Router(), CONSTANTS.WEB_HOST_PROXY + '/web-hosted/client-assets/dist'),
express.static(path.join(__dirname, `${hostPath}`, `assets`)) // "public" off of current is root
)
}
uiHostCreator('/ar', 'ar')
uiHostCreator('/de', 'de')
uiHostCreator('/es', 'es')
uiHostCreator('/fr', 'fr')
uiHostCreator('/fr-ca', 'fr-ca')
uiHostCreator('/nl', 'nl')
uiHostCreator('/zh-CN', 'zh-CN')
uiHostCreator('/ja', 'ja')
uiHostCreator('', 'en')
app.use(haltOnTimedOut)
app.use((err, req, res, next) => {
//check what error happened here ...
res.status(404).render(path.join(__dirname, `404.html`))
})
const port = CONSTANTS.PORTAL_PORT
app.listen(port, '0.0.0.0', err => {
console.error(err || 'No Error', `Server started at ${port}`)
})
function proxyCreator(route, baseUrl) {
route.all('/*', (req, res) => {
proxy.web(req, res, {
target: baseUrl,
})
})
return route
}
function uiHostCreator(hostPath, hostFolderName) {
app.use(
`${hostPath}`,
expressStaticGzip(path.join(__dirname, `www/${hostFolderName}`), {
enableBrotli: true,
orderPreference: ['br', 'gz'],
}),
)
app.get(`${hostPath}/*`, (req, res) => {
if (req.url.startsWith('/assets/')) {
res.sendFile(path.join(__dirname, `www/${hostFolderName}/${req.url}`))
} else if (req.url.startsWith('/.well-known/')) {
res.sendFile(path.join(__dirname, `${req.url}`))
} else {
res.sendFile(path.join(__dirname, `www/${hostFolderName}/index.html`))
}
})
}
function haltOnTimedOut(req, _res, next) {
if (!req.timedout) next()
}