Skip to content

Commit

Permalink
Add /api/mensa endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhorn committed Jan 9, 2021
1 parent aba7c05 commit b921a3f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
13 changes: 13 additions & 0 deletions rogue-thi-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rogue-thi-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"react-bootstrap": "^1.4.0",
"react-dom": "^17.0.1",
"react-leaflet": "^3.0.5",
"react-placeholder": "^4.0.3"
"react-placeholder": "^4.0.3",
"xml-js": "^1.6.11"
},
"devDependencies": {
"eslint": "^7.15.0",
Expand Down
31 changes: 31 additions & 0 deletions rogue-thi-app/pages/api/mensa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import xmljs from 'xml-js'
import MemoryCache from '../../lib/memory-cache'

const CACHE_TTL = 60 * 60 * 1000
const URL_DE = 'https://www.max-manager.de/daten-extern/sw-erlangen-nuernberg/xml/mensa-ingolstadt.xml'
const URL_EN = 'https://www.max-manager.de/daten-extern/sw-erlangen-nuernberg/xml/en/mensa-ingolstadt.xml'

const cache = new MemoryCache({ ttl: CACHE_TTL })

async function fetchPlan (lang) {
const url = (lang || 'de') === 'de' ? URL_DE : URL_EN

let plan = cache.get(url)

if (!plan) {
const resp = await fetch(url)
plan = xmljs.xml2js(await resp.text(), { compact: true })

cache.set(url, plan)
}

return plan
}

export default async function handler (req, res) {
const plan = await fetchPlan(req.query.lang)

res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(plan))
}

0 comments on commit b921a3f

Please sign in to comment.