-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.js
252 lines (222 loc) · 7.64 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
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
249
250
251
252
const debug = require('debug')('demspace:app')
const fs = require('fs')
const path = require('path')
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'))
const checkEngineVersion = require('check-node-version')
const { NODE_ENV, PORT, WWW_PORT } = process.env
const bodyParser = require('body-parser')
const callerPath = require('caller-path')
const compression = require('compression')
const cors = require('cors')
const cookieParser = require('cookie-parser')
const express = require('express')
const MemoryFS = require('memory-fs')
const resolveFrom = require('resolve-from')
const serveStatic = require('serve-static')
const webpack = require('webpack')
const { runtime } = require('raj')
// Ensure the correct node and NPM versions.
checkEngineVersion({
node: packageJson.engines.node,
npm: packageJson.engines.npm,
}, (error, result) => {
if (error) throw error
if (!result.versions.node.isSatisfied) {
throw new Error(`Invalid node version. Found: ${result.versions.node.version} / Wanted: ${result.versions.node.wanted}`)
}
if (!result.versions.npm.isSatisfied) {
throw new Error(`Invalid npm version. Found: ${result.versions.npm.version} / Wanted: ${result.versions.npm.wanted}`)
}
})
// transform dynamic import()'s in node environment to require()'s
// used by routes.js to load chunked JS bundles asynchronously.
require('@babel/register')({
ignore: [/node_modules/],
presets: [[require.resolve('@babel/preset-env'), { targets: { node: "8.9" } }]],
plugins: [
require.resolve('babel-plugin-dynamic-import-node'),
require.resolve('@babel/plugin-proposal-object-rest-spread'),
]
})
const { loadPage } = require('./helpers')
const webpackConfig = require('./webpack.config')
const twitterAvatarProxy = require('./middleware/twitter_avatar_proxy')
const imageProxy = require('./middleware/image_proxy')
const errorHandler = require('./middleware/error_handler')
const geoip = require('./middleware/geoip')
const eztextingWebhook = require('./middleware/eztexting_webhook')
const redirects = require('./middleware/redirects')
const twitterUsernameSearch = require('./middleware/twitter_username_search')
const verifyPhoneNumber = require('./middleware/verify_phone_number')
const geocode = require('./middleware/geocode')
const htmlWrapper = require('./views/html-wrapper')
let App = require('./app')
const port = PORT || WWW_PORT
const server = express()
const config = {
API_URL: process.env.API_URL,
APP_NAME: process.env.APP_NAME,
ASSETS_URL: process.env.ASSETS_URL,
GOOGLE_GEOCODER_KEY: process.env.GOOGLE_GEOCODER_KEY,
NODE_ENV: process.env.NODE_ENV,
WWW_URL: process.env.WWW_URL,
}
const statsOptions = {
assets: false,
builtAt: false,
chunks: false,
chunkModules: false,
chunksSort: '!size',
colors: true,
exclude: /node_modules|webpack|webpack.entry.js|browser.js|index.js/,
modulesSort: '!size',
version: false,
}
if (!Object.values(config).some(val => !!val)) {
throw new Error('Missing environment variables. Check .template.env for a reference of required variables.')
}
let started = false
const mfs = new MemoryFS()
const compiler = webpack(webpackConfig)
compiler.outputFileSystem = mfs
console.log('Building app...')
const compile = (done) => {
let init = false
if (NODE_ENV === 'production') {
compiler.run(done)
} else {
compiler.watch({ ignore: /node_modules/ }, (err, stats) => {
if (err || stats.compilation.errors.length) {
return console.error(err || stats.compilation.errors)
}
compiler.compiled = true
compiler.hash = stats.compilation.hash
if (!init) {
init = true
} else {
for (const moduleId of Object.keys(require.cache)) { /* eslint-disable-line no-restricted-syntax */
if (!~moduleId.indexOf('node_modules')) {
delete require.cache[resolveFrom(path.dirname(callerPath()), moduleId)]
}
}
App = require('./app')
}
done(null, stats)
})
}
}
let webpackStats = null
compile((err, stats) => {
if (err) return console.error(err)
webpackStats = stats
console.log(stats.toString({ ...statsOptions, context: __dirname }))
if (!started) {
started = true
startAppServer()
}
})
function contentType(filename) {
switch (path.extname(filename)) {
case '.css':
return 'text/css'
case '.js':
default:
return 'text/javascript'
}
}
function startAppServer() {
server
.disable('x-powered-by')
.enable('trust proxy') // use x-forwarded-by for request ip
.use(compression())
.use(cors())
.use(redirects)
if (NODE_ENV !== 'production') {
server.use(require('webpack-hot-middleware')(compiler, { log: false }))
}
server
.use('/assets', serveStatic(path.join(__dirname, 'assets'), { maxAge: '4h' }))
.get('/assets/:filename', (req, res) => {
mfs.readFile(`/${req.params.filename}`, 'utf8', (error, content) => {
if (error) return res.status(404).end()
res.setHeader('Content-Type', contentType(req.params.filename))
// Cache build JS/CSS for 3 months, since they use unique hashes for filenames.
res.setHeader('Cache-Control', `max-age=${90 * 24 * 60 * 60}`)
res.write(content)
res.end()
})
})
.get('/rpc/healthcheck', (req, res) => res.status(200).end())
.get('/rpc/geoip/:ip', geoip)
.get('/rpc/image-proxy/:url', imageProxy)
.get('/rpc/avatarsio/:username', twitterAvatarProxy)
.post('/rpc/verify_phone_number', bodyParser.json(), verifyPhoneNumber)
.get('/rpc/eztexting_webhook', eztextingWebhook)
.post('/rpc/twitter_username_search', bodyParser.json(), twitterUsernameSearch)
.post('/rpc/geocode', bodyParser.json(), geocode)
.use(cookieParser(), serveApp)
.use(errorHandler(htmlWrapper))
.listen(port, () => {
console.log(`App ready and listening on http://localhost:${port}`)
})
}
function serveApp(req, res, next) {
runApp(req, res, (error, html, status) => {
if (error) return next(error)
res.set('Content-Type', 'text/html')
res.status(status || 200).send(html)
})
}
function initAppState(App, req) {
return {
...App.init[0],
config,
location: {
...App.init[0].location,
ip: req.ip,
userAgent: req.get('User-Agent') || 'Unknown',
},
cookies: req.cookies,
}
}
function runApp(req, res, done) {
const initState = initAppState(App, req)
runtime({
...App,
init: [initState, (dispatch) => loadPage(req.url, 200, dispatch)],
update: (event, state) => {
debug(event, state)
switch (event.type) {
case 'cookieSet':
return [{
...state,
cookies: { ...state.cookies, [event.key]: event.value }
}, () => {
if (!res.appResponded) {
res.cookie(event.key, event.value, event.opts)
}
}]
case 'cookieUnset':
return [{ ...state, cookies: { ...state.cookies, [event.key]: null } }, () => {
if (!res.appResponded) {
res.clearCookie(event.key)
}
}]
default:
return App.update(event, state)
}
},
view: (state, dispatch) => {
if (!res.appResponded && !state.loading.page && state.view) {
res.appResponded = true
if (state.location.url !== req.url) {
return res.redirect(state.location.url)
}
const appHtml = App.view(state, dispatch)
const entries = webpackStats.compilation.entrypoints.get('main').getFiles().filter((file) => file.slice(-4) !== '.map')
const pageHtml = htmlWrapper(state, appHtml, entries.reverse())
done(null, pageHtml, state.location.status)
}
},
})
}