Skip to content
Snippets Groups Projects
Commit b8f5110b authored by bjoern.koester's avatar bjoern.koester :vulcan_tone1:
Browse files

add caching for manifests

parent e2ae46b3
No related branches found
No related tags found
No related merge requests found
......@@ -46,14 +46,25 @@ app.use('/swagger.json', (req, res) => res.json(swaggerDocument))
const urls = yaml.load(fs.readFileSync('./config/manifests/urls.yaml', 'utf8')).manifests
let manifestCache = []
let lastCached
const getManifests = async () => {
if (+new Date() < lastCached + 30000) return
const results = urls.map(url => fetch(url).then(result => {
if (!result.ok) throw new Error(`Failed to load manifest for url ${result.url} (Status: ${result.status}: ${result.statusText})`)
return result.json().catch(err => { throw new Error(`Failed to load manifest for url ${result.url}: ${err}`) })
}))
manifestCache = (await Promise.all(results)).flat()
lastCached = +new Date()
}
getManifests()
app.get('/api/manifest.json', async (req, res, next) => {
try {
const results = urls.map(url => fetch(url).then(res => {
if (!res.ok) throw new Error(`Failed to load manifest for url ${res.url} (Status: ${res.status}: ${res.statusText})`)
return res.json().catch(err => { throw new Error(`Failed to load manifest for url ${res.url}: ${err}`) })
}))
const content = await Promise.all(results)
res.json(content.flat() || [])
getManifests()
res.json(manifestCache || [])
} catch (err) {
next(err)
}
......
......@@ -13,6 +13,7 @@
"dependencies": {
"@cloudnative/health-connect": "^2.1.0",
"dotenv-defaults": "^2.0.1",
"eslint-config-standard": "^16.0.2",
"express": "^4.17.1",
"helmet": "^4.4.1",
"js-yaml": "^4.0.0",
......@@ -25,14 +26,12 @@
},
"devDependencies": {
"eslint": "^7.20.0",
"eslint-config-standard": "^16.0.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.3.1",
"eslint-plugin-standard": "^5.0.0",
"eslint-plugin-promise": "^5.1.0",
"husky": ">=4",
"lint-staged": ">=10",
"nodemon": "^1.19.4",
"nodemon": "^2.0.7",
"pino-pretty": "^4.7.1"
},
"husky": {
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment