Compare commits
No commits in common. "465ecf3fae2688196c6e34784c771f8eb4c4d387" and "b09955a4d442a6e135e0158ba543b0c21bf1428d" have entirely different histories.
465ecf3fae
...
b09955a4d4
185
index.js
185
index.js
@ -1,110 +1,110 @@
|
||||
const express = require("express");
|
||||
const axios = require("axios");
|
||||
const redis = require("redis");
|
||||
const cors = require("cors");
|
||||
const { param, validationResult } = require("express-validator");
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const redis = require('redis');
|
||||
const cors = require('cors');
|
||||
const { param, validationResult } = require('express-validator');
|
||||
const app = express();
|
||||
|
||||
// Redis setup
|
||||
const redisConfig = {
|
||||
url: process.env.REDIS_URL,
|
||||
database: 1,
|
||||
};
|
||||
url: process.env.REDIS_URL,
|
||||
db: 1,
|
||||
}
|
||||
|
||||
let redisClient;
|
||||
const client = redis.createClient(redisConfig);
|
||||
|
||||
(async () => {
|
||||
redisClient = redis.createClient(redisConfig);
|
||||
redisClient.on("error", (error) => console.error(`Error: ${error}`));
|
||||
await redisClient.connect();
|
||||
})();
|
||||
client.on('error', (err) => {
|
||||
console.log(err);
|
||||
});
|
||||
|
||||
// CORS
|
||||
app.use(cors());
|
||||
|
||||
// Functions
|
||||
|
||||
// Legacy fetch function
|
||||
function LeagueSeason(league, season) {
|
||||
this.url = "https://api-football-v1.p.rapidapi.com/v3/standings";
|
||||
this.method = "GET";
|
||||
this.params = {
|
||||
league: league,
|
||||
season: season,
|
||||
};
|
||||
function LeagueSeason(leagueID, year) {
|
||||
this.params = { league: leagueID, season: year }
|
||||
this.headers = {
|
||||
"X-RapidAPI-Host": process.env.API_HOST,
|
||||
"X-RapidAPI-Key": process.env.API_KEY,
|
||||
};
|
||||
}
|
||||
|
||||
// Fetch table from remote API
|
||||
async function fetchTable(league, season) {
|
||||
reqSeason = new LeagueSeason(league, season);
|
||||
const apiResponse = await axios.request(reqSeason);
|
||||
return apiResponse.data;
|
||||
}
|
||||
|
||||
// Get a league table
|
||||
async function getLeagueTable(req, res) {
|
||||
const league = req.params.league;
|
||||
const season = req.params.season;
|
||||
const seasontable = req.params.league + req.params.season;
|
||||
let results;
|
||||
let isCached = false;
|
||||
|
||||
// Try to fetch a league table
|
||||
try {
|
||||
// First check cache
|
||||
const cacheResults = await redisClient.get(seasontable);
|
||||
if (cacheResults) {
|
||||
isCached = true;
|
||||
results = JSON.parse(cacheResults);
|
||||
} else {
|
||||
// Fetch remotely
|
||||
results = await fetchTable(league, season);
|
||||
if (results.length === 0) {
|
||||
throw "API returned no data";
|
||||
}
|
||||
// Store table in cache
|
||||
await redisClient.set(seasontable, JSON.stringify(results), {
|
||||
EX: 21600,
|
||||
NX: true,
|
||||
});
|
||||
}
|
||||
// Return the league table for the corresponding season
|
||||
res.status(200).send({
|
||||
fromCache: isCached,
|
||||
table: results,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(404).send("Data not found");
|
||||
'x-rapidapi-host': process.env.API_HOST,
|
||||
'x-rapidapi-key': process.env.API_KEY,
|
||||
}
|
||||
}
|
||||
|
||||
// Routes
|
||||
// new method
|
||||
app.get("/league/:league/:season", getLeagueTable);
|
||||
|
||||
// Current Premier League Table
|
||||
app.get("/pl", getLeagueTable);
|
||||
app.get('/pl', async (req, res) => {
|
||||
const league = '39';
|
||||
const year = '2021';
|
||||
const query = league + year;
|
||||
try {
|
||||
client.get(query, async (err, leagueTable) => {
|
||||
if (err) throw err;
|
||||
|
||||
app.get(
|
||||
"/v2/:league/:year",
|
||||
// Return cached league table if present
|
||||
if (leagueTable) {
|
||||
res.status(200).send({
|
||||
table: JSON.parse(leagueTable),
|
||||
message: 'data retrieved from cache',
|
||||
});
|
||||
} else {
|
||||
// Fetch from the API
|
||||
reqSeason = new LeagueSeason(league, year);
|
||||
const leagueTable = await axios.get(
|
||||
'https://v3.football.api-sports.io/standings',
|
||||
reqSeason
|
||||
);
|
||||
// Save result to cache
|
||||
client.setex(query, 43200, JSON.stringify(leagueTable.data));
|
||||
// Return data from API
|
||||
res.status(200).send({
|
||||
table: leagueTable.data,
|
||||
message: 'cache miss',
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).send({ message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/championship', async (req, res) => {
|
||||
const league = '40';
|
||||
const year = '2021';
|
||||
const query = league + year;
|
||||
try {
|
||||
client.get(query, async (err, leagueTable) => {
|
||||
if (err) throw err;
|
||||
|
||||
// Return cached league table if present
|
||||
if (leagueTable) {
|
||||
res.status(200).send({
|
||||
table: JSON.parse(leagueTable),
|
||||
message: 'data retrieved from cache',
|
||||
});
|
||||
} else {
|
||||
// Fetch from the API
|
||||
reqSeason = new LeagueSeason(league, year);
|
||||
const leagueTable = await axios.get(
|
||||
'https://v3.football.api-sports.io/standings',
|
||||
reqSeason
|
||||
);
|
||||
// Save result to cache
|
||||
client.setex(league, 43200, JSON.stringify(leagueTable.data));
|
||||
// Return data from API
|
||||
res.status(200).send({
|
||||
table: leagueTable.data,
|
||||
message: 'cache miss',
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).send({ message: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/v2/:league/:year',
|
||||
[
|
||||
// League id must be one of these
|
||||
param("league").isIn(["39", "40", "41", "42"]),
|
||||
param('league').isIn(['39', '40', '41', '42']),
|
||||
// Year must be no earlier than 2018
|
||||
param("year").isIn([
|
||||
"2018",
|
||||
"2019",
|
||||
"2020",
|
||||
"2021",
|
||||
"2022",
|
||||
"2023",
|
||||
"2024",
|
||||
]),
|
||||
param('year').isIn(['2018', '2019', '2020', '2021']),
|
||||
],
|
||||
async (req, res) => {
|
||||
// Validate
|
||||
@ -124,13 +124,13 @@ app.get(
|
||||
if (leagueTable) {
|
||||
res.status(200).send({
|
||||
table: JSON.parse(leagueTable),
|
||||
message: "data retrieved from cache",
|
||||
message: 'data retrieved from cache',
|
||||
});
|
||||
} else {
|
||||
// Fetch from the API
|
||||
reqSeason = new LeagueSeason(league, year);
|
||||
const leagueTable = await axios.get(
|
||||
"https://v3.football.api-sports.io/standings",
|
||||
'https://v3.football.api-sports.io/standings',
|
||||
reqSeason
|
||||
);
|
||||
// Save result to cache
|
||||
@ -138,15 +138,14 @@ app.get(
|
||||
// Return data from API
|
||||
res.status(200).send({
|
||||
table: leagueTable.data,
|
||||
message: "cache miss",
|
||||
message: 'cache miss',
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).send({ message: err.message });
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
app.listen(process.env.PORT || 3001, () => {
|
||||
console.log(`Server running`);
|
||||
|
1832
package-lock.json
generated
1832
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "league_table",
|
||||
"version": "0.5.0",
|
||||
"version": "0.2.0",
|
||||
"engines": {
|
||||
"node": "20.x"
|
||||
"node": "18.x"
|
||||
},
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
@ -13,15 +13,14 @@
|
||||
"author": "nplayfair",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"dotenv": "^16.5.0",
|
||||
"eslint-import-resolver-node": "^0.3.9",
|
||||
"nodemon": "^3.1.10"
|
||||
"dotenv": "^16.3.1",
|
||||
"nodemon": "^3.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.9.0",
|
||||
"axios": "^1.6.2",
|
||||
"cors": "^2.8.5",
|
||||
"express": "^5.1.0",
|
||||
"express-validator": "^7.2.1",
|
||||
"redis": "^5.5.5"
|
||||
"express": "^4.18.2",
|
||||
"express-validator": "^7.0.1",
|
||||
"redis": "^4.6.11"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user