-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
server.js
155 lines (137 loc) · 3.98 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
149
150
151
152
153
154
155
/* eslint-disable no-undef */
import { createRequestHandler } from '@remix-run/express'
import compression from 'compression'
import dotenv from 'dotenv'
import express from 'express'
import helmet from 'helmet'
dotenv.config()
const viteDevServer =
process.env.NODE_ENV === 'production'
? undefined
: await import('vite').then((vite) =>
vite.createServer({
server: { middlewareMode: true },
}),
)
const remixHandler = createRequestHandler({
build: viteDevServer
? () => viteDevServer.ssrLoadModule('virtual:remix/server-build')
: // eslint-disable-next-line import/no-unresolved
await import('./build/server/index.js'), // comment necessary because lint runs before build
})
const app = express()
app.use(compression())
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
app.disable('x-powered-by')
const wsUrls = process.env.WS_URLS?.split(',').map((url) => url.trim())
const imgSrc = [
"'self'",
'data:',
'blob:',
'google.com',
'*.openstreetmap.org',
'firebasestorage.googleapis.com',
'onearmy.github.io',
'cdn.jsdelivr.net',
'*.google-analytics.com',
]
const cdnUrl = import.meta.env?.VITE_CDN_URL || process.env?.VITE_CDN_URL
if (cdnUrl) {
imgSrc.push(cdnUrl)
}
// helmet config
app.use(
helmet.contentSecurityPolicy({
directives: {
fontSrc: ["'self'", 'fonts.gstatic.com', 'fonts.googleapis.com'],
connectSrc: [
"'self'",
'*.run.app',
'securetoken.googleapis.com',
'firestore.googleapis.com',
'identitytoolkit.googleapis.com',
'*.openstreetmap.org',
'*.firebaseio.com',
'*.firebasedatabase.app',
'*.google-analytics.com',
'firebasestorage.googleapis.com',
'*.cloudfunctions.net',
'sentry.io',
...wsUrls,
],
defaultSrc: [
"'self'",
'googletagmanager.com',
'*.googletagmanager.com',
'analytics.google.com',
'*.analytics.google.com',
'*.google-analytics.com',
'*.firebaseio.com',
'googleapis.com',
],
scriptSrc: [
"'self'",
'googletagmanager.com',
'*.googletagmanager.com',
'fonts.gstatic.com',
'fonts.googleapis.com',
'*.analytics.google.com',
'*.google-analytics.com',
'www.youtube.com',
"'unsafe-eval'",
"'unsafe-inline'",
'*.firebasedatabase.app',
'*.firebaseio.com',
],
frameSrc: [
"'self'",
'onearmy.github.io',
'*.youtube.com',
'*.donorbox.org',
'donorbox.org',
'*.run.app',
'*.netlify.app',
],
imgSrc: imgSrc,
objectSrc: ["'self'"],
// Enforce HTTPS only on production
upgradeInsecureRequests:
process.env.NODE_ENV === 'production' ? [] : null,
},
}),
)
// Enforce HTTPS only on production
process.env.NODE_ENV === 'production' ??
app.use(
helmet.hsts({ maxAge: 31536000, preload: true, includeSubDomains: false }),
)
app.use(helmet.dnsPrefetchControl({ allow: true }))
app.use(helmet.hidePoweredBy())
app.use(helmet.noSniff())
app.use(helmet.referrerPolicy({ policy: ['origin'] }))
app.use(helmet.xssFilter())
app.use(helmet.hidePoweredBy())
app.use(function (req, res, next) {
if (!('JSONResponse' in res)) {
return next()
}
res.set('Cache-Control', 'public, max-age=31557600')
res.json(res.JSONResponse)
})
// handle asset requests
if (viteDevServer) {
app.use(viteDevServer.middlewares)
} else {
// Vite fingerprints its assets so we can cache forever.
app.use(
'/assets',
express.static('build/client/assets', { immutable: true, maxAge: '1y' }),
)
}
app.use(express.static('build/client', { maxAge: '1h' }))
app.all('*', remixHandler)
let port = process.env.PORT || 3456 // 3456 is default port for ci
app.listen(port, '0.0.0.0', () => {
// eslint-disable-next-line no-console, no-undef
console.log(`Express server started on http://0.0.0.0:${port}`)
})