1
0
mirror of https://github.com/oceanprotocol/webtasks synced 2024-06-15 17:03:15 +02:00

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

View File

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