diff --git a/server.js b/server.js index 7a6fa50..339948e 100644 --- a/server.js +++ b/server.js @@ -5,13 +5,24 @@ import { dirname, join } from 'path'; const __dirname = dirname(fileURLToPath(import.meta.url)); const app = express(); +const cache = new Map(); +const TTL = 60 * 60 * 1000; // 1 hour + +async function cachedFetch(url) { + const entry = cache.get(url); + if (entry && Date.now() - entry.ts < TTL) return entry.data; + const response = await fetch(url); + const data = await response.json(); + cache.set(url, { data, ts: Date.now() }); + return data; +} + app.use(express.static(join(__dirname, 'public'))); app.get('/api/holidays', async (req, res) => { const { year } = req.query; try { - const response = await fetch(`https://api.dagsmart.se/holidays?year=${year}&weekends=false`); - const data = await response.json(); + const data = await cachedFetch(`https://api.dagsmart.se/holidays?year=${year}&weekends=false`); res.json(data); } catch (err) { res.status(500).json({ error: String(err) }); @@ -21,8 +32,7 @@ app.get('/api/holidays', async (req, res) => { app.get('/api/bridge-days', async (req, res) => { const { year } = req.query; try { - const response = await fetch(`https://api.dagsmart.se/bridge-days?year=${year}`); - const data = await response.json(); + const data = await cachedFetch(`https://api.dagsmart.se/bridge-days?year=${year}`); res.json(data); } catch (err) { res.status(500).json({ error: String(err) });