-
Notifications
You must be signed in to change notification settings - Fork 64
/
server.js
127 lines (104 loc) · 2.75 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
/* eslint-disable no-restricted-globals */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
/* eslint-disable prefer-destructuring */
/* eslint-disable import/first */
/* eslint-disable new-cap */
const { EventEmitter: EE } = require('events')
const express = require('express')
const fileModule = require('file')
const statuses = require('statuses')
const debug = require('debug')('server')
const Raven = require('raven')
const metricsMiddleware = require('./lib/expressMetrics')
const cache = require('./lib/cache')
const cors = require('./lib/cors')
const app = express()
// Set up error tracking with Sentry
const SENTRY_URL = process.env.SENTRY_URL
const redis = require('./lib/redis')
if (SENTRY_URL !== undefined && SENTRY_URL !== '') {
Raven.config(SENTRY_URL).install()
// The request handler must be the first middleware on the app
app.use(Raven.requestHandler())
// The error handler must be before any other error middleware
app.use(Raven.errorHandler())
}
if (process.env.NODE_ENV !== 'test') {
app.use(metricsMiddleware())
app.get('/metrics', (req, res) => {
redis.HGETALL('metrics', (error, metrics) => {
res.json(metrics)
})
})
}
module.exports = app
/**
* Set the spacing to 0 for shorter output
*/
app.set('json spaces', 0)
/**
* Create an event listener for app
*/
EE.call(app)
/**
* Cross-origin resource sharing
*/
app.use(cors())
/**
* Caching layer
*/
app.use(cache())
/**
* Set up endpoints
*/
fileModule.walkSync('./endpoints', (dirPath, dirs, endpoints) => {
function requireEndpoint(endpoint) {
if (endpoint.includes('.js') && endpoint !== 'graphql_schema.js') {
try {
require(`./${dirPath}/${endpoint}`)
} catch (e) {
console.error('Error loading file', e)
}
}
}
if (endpoints && dirPath.indexOf('test') < 0) {
endpoints.forEach(requireEndpoint)
}
})
app.use((error, req, res, next) => {
let code = 500
let message = 'Unknown error'
if (res.headersSent) {
console.error('Headers already sent')
return next()
}
if (typeof error === 'number') {
code = error
message = statuses[code] || message
} else if (
typeof error === 'object' &&
error.message &&
error.message.length === 3 &&
!isNaN(error.message)) {
code = error.message
message = statuses[code] || message
} else {
// Other errors that might have been swallowed
debug(error.stack)
message = error.message
}
code = parseInt(code, 10)
res.status(code).json({ error: message })
})
/**
* Start the server
*/
const port =
process.env.PORT || (process.env.NODE_ENV === 'test' ? 3101 : 3100)
app.listen(port, () => {
app.emit('ready')
})
app.on('ready', () => {
debug('Server running at port:', port)
})