-
Notifications
You must be signed in to change notification settings - Fork 35
/
dataRouter.js
64 lines (52 loc) · 2.06 KB
/
dataRouter.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
const express = require('../../ExpressApp')
const router = express.Router()
const { AcquireTimeoutError } = require('../../services/MutexService')
const getData = async (req, res, name, data) => {
try {
return res.json(data)
} catch (e) {
if (e instanceof AcquireTimeoutError) {
return res.status(503).json({ error: 'timeout reached' })
}
console.error('[error] dataRouter::get:' + name + '.json failed with "' + e.toString() + '"')
if (!res.headersSent) {
return res.status(500).json({ error: 'unexpected error' })
}
throw e
}
}
router.get('/newshub.json', async (req, res) => {
getData(req, res, 'newshub', await req.appContainer.get('WordpressService').getNewshub())
})
router.get('/tournament-news.json', async (req, res) => {
getData(req, res, 'tournament-news', await req.appContainer.get('WordpressService').getTournamentNews())
})
router.get('/faf-teams.json', async (req, res) => {
getData(req, res, 'faf-teams', await req.appContainer.get('WordpressService').getFafTeams())
})
router.get('/content-creators.json', async (req, res) => {
getData(req, res, 'content-creators', await req.appContainer.get('WordpressService').getContentCreators())
})
router.get('/recent-players.json', async (req, res) => {
const rawData = await req.appContainer.get('LeaderboardService').getLeaderboard(1)
const data = rawData.map((item) => ({
id: item.playerId,
name: item.label
}))
getData(req, res, 'content-creators', data)
})
router.get('/clans.json', async (req, res) => {
try {
return res.json(await req.appContainer.get('ClanService').getAll())
} catch (e) {
if (e instanceof AcquireTimeoutError) {
return res.status(503).json({ error: 'timeout reached' })
}
console.error('[error] dataRouter::get:clans.json failed with "' + e.toString() + '"')
if (!res.headersSent) {
return res.status(500).json({ error: 'unexpected error' })
}
throw e
}
})
module.exports = router