add simple caching

This commit is contained in:
Matthias Kretschmann 2018-10-17 14:40:14 +02:00
parent ee91c4795d
commit 94b802a7a3
Signed by: m
GPG Key ID: 606EEEF3C479A91F
1 changed files with 15 additions and 3 deletions

View File

@ -6,15 +6,17 @@ const webtask = require('webtask-tools')
const app = express()
// Just chained callbacks in lack of proper async/await support on webtask.io
app.get('/', (req, res) => {
let cached
const getData = res => {
// Just chained callbacks in lack of proper async/await support on webtask.io
// Gitcoin bounties
request('https://gitcoin.co/api/v0.1/bounties/', (error, response, body) => {
if (error) return error
const gitcoinBody = JSON.parse(body) // returns only open bounties by default
const gitcoin = gitcoinBody.filter(
// filter the response manually, no way atm to do that as API query
// filter the response manually, no way atm to do that as API query
item => item.funding_organisation.includes('Ocean Protocol')
)
@ -29,10 +31,20 @@ app.get('/', (req, res) => {
const bountiesNetwork = JSON.parse(body)
holder.bountiesNetwork = bountiesNetwork.results
cached = holder
// Send final response
res.send(holder)
})
})
}
app.get('/', (req, res) => {
if (cached) {
res.send(cached)
} else {
getData(res)
}
})
module.exports = webtask.fromExpress(app)