forked from badgen/badgen.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.js
51 lines (45 loc) · 1.36 KB
/
service.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
const micro = require('micro')
const { router, get } = require('micro-fork')
const publicHandlers = require('./libs/public-handlers.js')
const serveIndex = require('./libs/serve-index.js')
const serve404 = require('./libs/serve-404.js')
const serveBadge = require('./libs/serve-badge.js')
const serveMetadata = require('./libs/serve-metadata.js')
const liveHandlers = require('./libs/live-handlers.js')
const serveApi = require('./libs/serve-api.js')
const raven = require('./libs/raven.js')
const indexHandler = (req, res) => {
if (req.headers.host === 'badgen.now.sh') {
res.setHeader('Location', 'https://badgen.net')
micro.send(res, 301)
} else {
serveIndex(req, res)
}
}
const serveStaticBadge = (req, res) => {
serveBadge(req, res, { maxAge: '31536000' })
}
const main = router()(
get('/*', serve404),
get('/', indexHandler),
get('/metadata.json', serveMetadata),
get('/badge/:subject/:status', serveStaticBadge),
get('/badge/:subject/:status/:color', serveStaticBadge),
...liveHandlers,
...publicHandlers
)
module.exports = (req, res) => {
switch (req.headers.host) {
case 'api.badgen.net':
case '127.0.0.1:3000':
return serveApi(req, res)
default:
return main(req, res)
}
}
const autoRun = () => {
if (require.main === module) {
micro(module.exports).listen(3000)
}
}
raven ? raven.context(autoRun) : autoRun()