2019-05-14 20:52:48 +02:00
|
|
|
const ms = require('ms')
|
2019-06-06 20:29:39 +02:00
|
|
|
const { logError } = require('./utils')
|
2019-05-14 20:52:48 +02:00
|
|
|
|
|
|
|
const fetchGitHubRepos = require('./networks/github')
|
|
|
|
const fetchBounties = require('./networks/bounties')
|
|
|
|
const fetchMedium = require('./networks/medium')
|
2019-05-14 20:59:15 +02:00
|
|
|
const fetchTwitter = require('./networks/twitter')
|
2019-05-14 21:57:08 +02:00
|
|
|
const fetchTelegram = require('./networks/telegram')
|
2019-05-14 20:52:48 +02:00
|
|
|
|
|
|
|
let cacheGithub = null
|
|
|
|
let cacheBounties = null
|
|
|
|
let cacheMedium = null
|
2019-05-14 20:59:15 +02:00
|
|
|
let cacheTwitter = null
|
2019-05-14 21:57:08 +02:00
|
|
|
let cacheTelegram = null
|
2019-05-14 20:52:48 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Create the response
|
|
|
|
//
|
|
|
|
module.exports = async (req, res) => {
|
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET')
|
|
|
|
|
|
|
|
try {
|
2019-06-24 17:25:41 +02:00
|
|
|
/* eslint-disable require-atomic-updates */
|
2019-05-14 20:52:48 +02:00
|
|
|
if (!cacheGithub || Date.now() - cacheGithub.lastUpdate > ms('5m')) {
|
|
|
|
cacheGithub = await fetchGitHubRepos()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cacheBounties || Date.now() - cacheBounties.lastUpdate > ms('5m')) {
|
|
|
|
cacheBounties = await fetchBounties()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cacheMedium || Date.now() - cacheMedium.lastUpdate > ms('5m')) {
|
|
|
|
cacheMedium = await fetchMedium()
|
|
|
|
}
|
2019-05-14 20:59:15 +02:00
|
|
|
|
|
|
|
if (!cacheTwitter || Date.now() - cacheTwitter.lastUpdate > ms('5m')) {
|
|
|
|
cacheTwitter = await fetchTwitter()
|
|
|
|
}
|
2019-05-14 21:57:08 +02:00
|
|
|
|
|
|
|
if (!cacheTelegram || Date.now() - cacheTelegram.lastUpdate > ms('5m')) {
|
|
|
|
cacheTelegram = await fetchTelegram()
|
|
|
|
}
|
2019-06-24 17:25:41 +02:00
|
|
|
/* eslint-enable require-atomic-updates */
|
2019-05-14 20:52:48 +02:00
|
|
|
} catch (error) {
|
|
|
|
logError(error.message)
|
|
|
|
}
|
|
|
|
|
2019-08-14 14:22:32 +02:00
|
|
|
res.send({
|
2019-05-14 20:52:48 +02:00
|
|
|
github: cacheGithub,
|
|
|
|
bounties: cacheBounties,
|
2019-05-14 20:59:15 +02:00
|
|
|
medium: cacheMedium,
|
2019-05-14 21:57:08 +02:00
|
|
|
twitter: cacheTwitter,
|
|
|
|
telegram: cacheTelegram
|
2019-08-14 14:22:32 +02:00
|
|
|
})
|
2019-05-14 20:52:48 +02:00
|
|
|
}
|