ardasen commited on
Commit
6f6e5ac
·
verified ·
1 Parent(s): 4c9ae8b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +16 -0
server.js CHANGED
@@ -57,6 +57,7 @@ cron.schedule('0 */12 * * *', async () => {
57
  // Add more endpoints to update cache as needed
58
  });
59
 
 
60
  // Generic endpoint handler to fetch data with caching and error handling
61
  const handleEndpointWithCache = async (req, res, url, cacheKey) => {
62
  try {
@@ -79,6 +80,21 @@ const handleEndpointWithCache = async (req, res, url, cacheKey) => {
79
  }
80
  };
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  // Example endpoint to get current season data
83
  app.get('/api/currentSeason', async (req, res) => {
84
  await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}current.json`, 'currentSeason');
 
57
  // Add more endpoints to update cache as needed
58
  });
59
 
60
+
61
  // Generic endpoint handler to fetch data with caching and error handling
62
  const handleEndpointWithCache = async (req, res, url, cacheKey) => {
63
  try {
 
80
  }
81
  };
82
 
83
+ // Endpoint to manually trigger cache update
84
+ app.get('/api/updateCache', async (req, res) => {
85
+ const { url, cacheKey } = req.query;
86
+
87
+ if (!url || !cacheKey) {
88
+ return res.status(400).send('url and cacheKey query parameters are required.');
89
+ }
90
+
91
+ try {
92
+ await fetchDataAndUpdateCache(url, cacheKey);
93
+ res.send(`Cache for ${cacheKey} updated successfully.`);
94
+ } catch (error) {
95
+ res.status(500).send(`Error updating cache for ${cacheKey}.`);
96
+ }
97
+ });
98
  // Example endpoint to get current season data
99
  app.get('/api/currentSeason', async (req, res) => {
100
  await handleEndpointWithCache(req, res, `${ERGAST_API_BASE_URL}current.json`, 'currentSeason');