2018-08-05 00:40:10 +00:00
|
|
|
const axios = require('./axios.js')
|
|
|
|
const { get } = require('micro-fork')
|
|
|
|
const liveFns = require('./live-fns/_index.js')
|
|
|
|
const serveBadge = require('./serve-badge.js')
|
|
|
|
const liveFetcher = require('./live-fetcher.js')
|
|
|
|
|
|
|
|
const { API_HOST } = process.env
|
2018-08-11 13:09:55 +00:00
|
|
|
const apiFetcher = async url => {
|
2018-08-11 13:20:59 +00:00
|
|
|
return axios.get(API_HOST + url).then(
|
2018-08-11 13:09:55 +00:00
|
|
|
res => res.data,
|
|
|
|
err => {
|
|
|
|
console.error('API_ERR', url, err.message)
|
2018-08-11 13:20:59 +00:00
|
|
|
return { ...err.response.data, httpCode: 200 }
|
2018-08-11 13:09:55 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2018-08-05 00:40:10 +00:00
|
|
|
|
|
|
|
module.exports = Object.entries(liveFns).map(([name, fn]) => {
|
|
|
|
return get(`/${name}/*`, async (req, res) => {
|
|
|
|
const {
|
|
|
|
subject = name,
|
|
|
|
status = 'unknown',
|
|
|
|
color = 'grey',
|
2018-08-11 13:09:55 +00:00
|
|
|
failed = false,
|
2018-08-11 13:20:59 +00:00
|
|
|
httpCode = 200
|
2018-08-11 13:09:55 +00:00
|
|
|
} = await (
|
|
|
|
API_HOST
|
|
|
|
? apiFetcher(req.url)
|
|
|
|
: liveFetcher(name, fn, req.params['*'])
|
2018-08-05 00:40:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const style = req.headers.host === 'flat.badgen.net' ? 'flat' : undefined
|
|
|
|
req.params = { subject, status, color, style }
|
|
|
|
serveBadge(req, res, {
|
2018-08-11 13:20:59 +00:00
|
|
|
code: httpCode,
|
2018-08-05 00:40:10 +00:00
|
|
|
maxAge: failed ? '0' : (Math.random() * 60 + 60).toFixed()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|