|
|
const express = require('express'); |
|
|
const axios = require('axios'); |
|
|
const NodeCache = require('node-cache'); |
|
|
const cron = require('node-cron'); |
|
|
const cors = require('cors'); |
|
|
|
|
|
const app = express(); |
|
|
const port = process.env.PORT || 7860; |
|
|
const cache = new NodeCache(); |
|
|
|
|
|
const ERGAST_API_BASE_URL = 'https://ergast.com/api/f1/'; |
|
|
|
|
|
app.use(cors()); |
|
|
|
|
|
|
|
|
const fetchDataAndUpdateCache = async (url, cacheKey) => { |
|
|
try { |
|
|
const response = await axios.get(url); |
|
|
cache.set(cacheKey, response.data); |
|
|
console.log(`Updated ${cacheKey} cache`); |
|
|
} catch (error) { |
|
|
console.error(`Error updating ${cacheKey} cache:`, error); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
cron.schedule('0 */12 * * *', async () => { |
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}current.json`, 'currentSeason'); |
|
|
|
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}current/driverStandings.json`, 'driverStandings-current'); |
|
|
|
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}2024/driverStandings.json`, 'driverStandings-2024'); |
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}2024/driverStandings.json`, 'driverStandings'); |
|
|
|
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}current/constructorStandings.json`, 'constructorStandings-current'); |
|
|
|
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}2024/constructorStandings.json`, 'constructorStandings-2024'); |
|
|
|
|
|
await fetchDataAndUpdateCache(`${ERGAST_API_BASE_URL}2024/constructorStandings.json`, 'constructorStandings'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const handleEndpointWithCache = async (req, res, url, cacheKey) => { |
|
|
try { |
|
|
const cachedData = cache.get(cacheKey); |
|
|
if (cachedData) { |
|
|
res.json(cachedData); |
|
|
} else { |
|
|
const response = await axios.get(url); |
|
|
cache.set(cacheKey, response.data); |
|
|
res.json(response.data); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error(`Error fetching ${cacheKey} from Ergast API:`, error); |
|
|
const cachedData = cache.get(cacheKey); |
|
|
if (cachedData) { |
|
|
res.json(cachedData); |
|
|
} else { |
|
|
res.status(500).send(`Error fetching data from Ergast API for ${cacheKey} and no cache available.`); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
app.get('/api/updateCache', async (req, res) => { |
|
|
const { url, cacheKey } = req.query; |
|
|
|
|
|
if (!url || !cacheKey) { |
|
|
return res.status(400).send('url and cacheKey query parameters are required.'); |
|
|
} |
|
|
|
|
|
try { |
|
|
await fetchDataAndUpdateCache(url, cacheKey); |
|
|
res.send(`Cache for ${cacheKey} updated successfully.`); |
|
|
} catch (error) { |
|
|
res.status(500).send(`Error updating cache for ${cacheKey}.`); |
|
|
} |
|
|
}); |
|
|
|
|
|
app.get('/api/currentSeason', async (req, res) => { |
|
|
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}current.json`, 'currentSeason'); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/api/driverStandings', async (req, res) => { |
|
|
const season = req.query.season || 'current'; |
|
|
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}${season}/driverStandings.json`, `driverStandings-${season}`); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/api/constructorStandings', async (req, res) => { |
|
|
const season = req.query.season || 'current'; |
|
|
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}${season}/constructorStandings.json`, `constructorStandings-${season}`); |
|
|
}); |
|
|
|
|
|
|
|
|
app.get('/api/raceResults', async (req, res) => { |
|
|
const season = req.query.season || 'current'; |
|
|
const round = req.query.round; |
|
|
if (!round) { |
|
|
return res.status(400).send('Round is required.'); |
|
|
} |
|
|
await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}${season}/${round}/results.json`, `raceResults-${season}-${round}`); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.listen(port, () => { |
|
|
console.log(`Server running on port ${port}`); |
|
|
}); |
|
|
|