-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
161 lines (124 loc) · 3.65 KB
/
index.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
const micro = require('micro')
const {get, post, router} = require('microrouter')
const {enqueue, configureQueues, joinJobQueue, disconnectQueues} = require('bull-manager')
const sentry = require('./lib/utils/sentry')
const mongo = require('./lib/utils/mongo')
const createRedis = require('./lib/utils/redis')
const jobs = require('./jobs/definitions')
const {findLink, upsertLink, getLinkSummary} = require('./lib/link')
const {getLinkChecks, getLinkCheck, findLastNonRunningCheck} = require('./lib/check')
function handleErrors(next) {
return async (req, res) => {
try {
return await next(req, res)
} catch (error) {
if (error.statusCode) {
if (error.originalError) {
sentry.captureException(error)
}
return micro.send(res, error.statusCode, {
error: error.message
})
}
sentry.captureException(error)
return micro.send(res, 500, {
error: 'an unexpected error happened, we have been notified'
})
}
}
}
const routes = router(
get('/', async (req, res) => {
if (!req.query.location) {
throw micro.createError(400, 'location is required')
}
const link = await findLink(req.query.location)
if (!link) {
throw micro.createError(404, `link with location ${req.query.location} was not found`)
}
res.statusCode = 302
res.setHeader('Location', `/${link._id}`)
res.end()
}),
get('/:link/checks/latest', async (req, res) => {
const check = await findLastNonRunningCheck(req.params.link)
if (!check) {
throw micro.createError(404, `latest check for link ${req.params.link} was not found`)
}
res.statusCode = 302
res.setHeader('Location', `/${req.params.link}/checks/${check.number}`)
res.end()
}),
get('/:link/checks/:number', async req => {
const check = await getLinkCheck(req.params.link, req.params.number)
if (!check) {
throw micro.createError(404, `check number ${req.params.number} for link ${req.params.link} was not found`)
}
return check
}),
get('/:link/checks', async req => {
const checks = await getLinkChecks(req.params.link)
if (!checks) {
throw micro.createError(404, `link with id ${req.params.link} was not found`)
}
return checks
}),
get('/:link', async req => {
const summary = await getLinkSummary(req.params.link)
if (!summary) {
throw micro.createError(404, `link with id ${req.params.link} was not found`)
}
return summary
}),
post('/', async req => {
const json = await micro.json(req)
if (!json.location) {
throw micro.createError(400, 'location is required')
}
const link = await upsertLink(json.location)
await enqueue('check', json.location, {
location: json.location,
linkId: link._id,
options: {
noCache: Boolean(json.noCache)
}
})
return link
})
)
const server = micro(handleErrors(routes))
async function main() {
const port = process.env.PORT || 5000
await mongo.connect()
await mongo.ensureIndexes()
await server.listen(port)
configureQueues({
createRedis: createRedis({
onError: shutdown
}),
prefix: 'link-proxy'
})
await Promise.all(
jobs.map(job => {
return joinJobQueue(job.name, job.options)
})
)
mongo.client.on('close', () => {
shutdown(new Error('Mongo connection was closed'))
})
console.log(`Server running on port ${port}`)
}
main().catch(error => {
shutdown(error)
})
async function shutdown(err) {
await Promise.all([
server.close(),
disconnectQueues(),
mongo.disconnect()
])
if (err) {
sentry.captureException(err)
process.exit(1)
}
}