forked from streetmix/streetmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
248 lines (210 loc) · 7.66 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Run this before other modules
if (process.env.NEW_RELIC_LICENSE_KEY) {
require('newrelic')
}
process.title = 'streetmix'
const compression = require('compression')
const cookieParser = require('cookie-parser')
const cookieSession = require('cookie-session')
const express = require('express')
const cors = require('cors')
const helmet = require('helmet')
const config = require('config')
const path = require('path')
const uuid = require('uuid/v4')
const controllers = require('./app/controllers')
const resources = require('./app/resources')
const requestHandlers = require('./lib/request_handlers')
const initRedisClient = require('./lib/redis')
const initMongoDB = require('./lib/db')
const exec = require('child_process').exec
const client = initRedisClient()
initMongoDB()
const app = module.exports = express()
// Get the timestamp of this server's start time to use as a cachebusting filename.
const cacheTimestamp = Date.now()
app.locals.cacheTimestamp = cacheTimestamp
process.on('uncaughtException', function (error) {
console.log(error)
console.trace()
if (client.connected) {
client.on('end', function () {
process.exit(1)
})
} else {
process.exit(1)
}
})
// Provide a message after a Ctrl-C
// Note: various sources tell us that this does not work on Windows
process.on('SIGINT', function () {
if (app.locals.config.env === 'development') {
console.log('Stopping Streetmix!')
exec('npm stop')
}
if (client.connected) {
client.on('end', process.exit)
} else {
process.exit()
}
})
app.locals.config = config
// Not all headers from `helmet` are on by default. These turns on specific
// off-by-default headers for better security as recommended by https://securityheaders.io/
const helmetConfig = {
frameguard: false, // Allow Streetmix to be iframed in 3rd party sites
hsts: {
maxAge: 5184000, // 60 days
includeSubDomains: false // we don't have a wildcard ssl cert
},
referrerPolicy: {
policy: 'no-referrer-when-downgrade'
}
}
// CSP directives are defined separately so we can generate nonces
const csp = {
directives: {
defaultSrc: ["'self'"],
styleSrc: [
"'self'",
"'unsafe-inline'",
'fonts.googleapis.com',
'*.typekit.net'
],
scriptSrc: [
"'self'",
'platform.twitter.com',
'https://www.google-analytics.com',
'cdn.mxpnl.com',
'streetmix.auth0.com',
'*.basemaps.cartocdn.com',
'api.geocode.earth',
(req, res) => "'nonce-" + res.locals.nonce.google_analytics + "'",
(req, res) => "'nonce-" + res.locals.nonce.mixpanel + "'"
],
childSrc: ['platform.twitter.com'],
frameSrc: ["'self'", 'streetmix.github.io'],
imgSrc: [
"'self'",
'data:',
'pbs.twimg.com',
'syndication.twitter.com',
's.gravatar.com',
'https://www.google-analytics.com',
'*.basemaps.cartocdn.com'
],
fontSrc: [
"'self'",
'fonts.gstatic.com',
'*.typekit.net'
],
connectSrc: [
"'self'",
'api.mixpanel.com',
'api.geocode.earth',
'syndication.twitter.com',
'https://www.google-analytics.com',
'app.getsentry.com',
'streetmix.auth0.com'
]
}
}
// Allow arbitrary injected code (e.g. Redux dispatches from dev tools) in development
if (app.locals.config.env === 'development') {
csp.directives.scriptSrc.push("'unsafe-eval'")
// Allows websockets for hot-module reloading (note: ports are assigned randomly by Parcel)
csp.directives.connectSrc.push('ws:')
}
app.use(helmet(helmetConfig))
app.use(express.json())
app.use(compression())
app.use(cookieParser())
app.use(cookieSession({ secret: config.cookie_session_secret }))
app.use(requestHandlers.login_token_parser)
app.use(requestHandlers.request_log)
app.use(requestHandlers.request_id_echo)
// Generate nonces for inline scripts
app.use(function (req, res, next) {
res.locals.nonce = {
google_analytics: uuid(),
mixpanel: uuid()
}
next()
})
// Set Redis client for when requesting the geoip
app.use('/services/geoip', function (req, res, next) {
req.redisClient = client
next()
})
// Set CSP directives
app.use(helmet.contentSecurityPolicy(csp))
// Rewrite requests with timestamp
app.use(function (req, res, next) {
// Matches a filename like styles.2395934243.css
// Accepts optional `?29090424` query string used by Parcel's hot-module reloader
req.url = req.url.replace(/\/([^/]+)\.[0-9]+\.(css|js)(\?[0-9]+)?$/, '/$1.$2')
next()
})
app.set('view engine', 'hbs')
app.set('views', path.join(__dirname, '/app/views'))
app.get('/help/about', function (req, res) {
res.redirect('https://www.opencollective.com/streetmix/')
})
app.get('/map', function (req, res) {
res.redirect('https://streetmix.github.io/map/')
})
app.get('/privacy-policy', express.static(path.join(__dirname, '/public/pages'), { fallthrough: false }))
app.get('/terms-of-service', express.static(path.join(__dirname, '/public/pages'), { fallthrough: false }))
app.get('/twitter-sign-in', controllers.twitter_sign_in.get)
app.get(config.twitter.oauth_callback_uri, controllers.twitter_sign_in_callback.get)
// Auth0
app.get(config.auth0.callback_uri, controllers.auth0_sign_in_callback.get)
app.options('/api/v1/users', cors())
app.post('/api/v1/users', cors(), resources.v1.users.post)
app.get('/api/v1/users', cors(), resources.v1.users.get)
app.options('/api/v1/users/:user_id', cors()) // Enable pre-flight request for authorized PUT request
app.get('/api/v1/users/:user_id', cors(), resources.v1.user.get)
app.put('/api/v1/users/:user_id', cors(), resources.v1.user.put)
app.delete('/api/v1/users/:user_id', cors(), resources.v1.user.delete)
app.options('/api/v1/users/:user_id/login-token', cors())
app.delete('/api/v1/users/:user_id/login-token', cors(), resources.v1.user_session.delete)
app.options('/api/v1/users/:user_id/streets', cors())
app.delete('/api/v1/users/:user_id/streets', cors(), resources.v1.users_streets.delete)
app.get('/api/v1/users/:user_id/streets', cors(), resources.v1.users_streets.get)
app.post('/api/v1/streets', resources.v1.streets.post)
app.get('/api/v1/streets', resources.v1.streets.find)
app.head('/api/v1/streets', resources.v1.streets.find)
app.delete('/api/v1/streets/:street_id', resources.v1.streets.delete)
app.head('/api/v1/streets/:street_id', resources.v1.streets.get)
app.get('/api/v1/streets/:street_id', resources.v1.streets.get)
app.put('/api/v1/streets/:street_id', resources.v1.streets.put)
app.get('/api/v1/geo', cors(), resources.v1.geo.get)
app.get('/services/geoip', resources.services.geoip.get)
app.get('/api/v1/translate/:locale_code/:resource_name', resources.v1.translate.get)
app.get('/api/v1/flags', cors(), resources.v1.flags.get)
// Catch all for all broken api paths, direct to 404 response.
app.get('/api/*', function (req, res) {
res.status(404).json({ status: 404, error: 'Not found. Did you mispell something?' })
})
// SVG bundled images served directly from packages
app.get('/assets/images/icons.svg', function (req, res) {
res.sendFile(path.join(__dirname, '/node_modules/@streetmix/icons/dist/icons.svg'))
})
app.get('/assets/images/images.svg', function (req, res) {
res.sendFile(path.join(__dirname, '/node_modules/@streetmix/illustrations/dist/images.svg'))
})
app.use('/assets', express.static(path.join(__dirname, '/build')))
app.use(express.static(path.join(__dirname, '/public')))
// Catch all for all broken assets, direct to 404 response.
app.get('/assets/*', function (req, res) {
res.status(404).render('404', {})
})
// Allow hot-module reloading (HMR) in non-production environments
if (config.env !== 'production') {
const runBundle = require('./app/bundle')
runBundle(app)
}
// Catch-all
app.use(function (req, res) {
res.render('main', {})
})