Compare commits
No commits in common. "465ecf3fae2688196c6e34784c771f8eb4c4d387" and "b09955a4d442a6e135e0158ba543b0c21bf1428d" have entirely different histories.
465ecf3fae
...
b09955a4d4
183
index.js
183
index.js
@ -1,110 +1,110 @@
|
|||||||
const express = require("express");
|
const express = require('express');
|
||||||
const axios = require("axios");
|
const axios = require('axios');
|
||||||
const redis = require("redis");
|
const redis = require('redis');
|
||||||
const cors = require("cors");
|
const cors = require('cors');
|
||||||
const { param, validationResult } = require("express-validator");
|
const { param, validationResult } = require('express-validator');
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
// Redis setup
|
|
||||||
const redisConfig = {
|
const redisConfig = {
|
||||||
url: process.env.REDIS_URL,
|
url: process.env.REDIS_URL,
|
||||||
database: 1,
|
db: 1,
|
||||||
};
|
}
|
||||||
|
|
||||||
let redisClient;
|
const client = redis.createClient(redisConfig);
|
||||||
|
|
||||||
(async () => {
|
client.on('error', (err) => {
|
||||||
redisClient = redis.createClient(redisConfig);
|
console.log(err);
|
||||||
redisClient.on("error", (error) => console.error(`Error: ${error}`));
|
});
|
||||||
await redisClient.connect();
|
|
||||||
})();
|
|
||||||
|
|
||||||
// CORS
|
// CORS
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
// Functions
|
function LeagueSeason(leagueID, year) {
|
||||||
|
this.params = { league: leagueID, season: year }
|
||||||
// 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,
|
|
||||||
};
|
|
||||||
this.headers = {
|
this.headers = {
|
||||||
"X-RapidAPI-Host": process.env.API_HOST,
|
'x-rapidapi-host': process.env.API_HOST,
|
||||||
"X-RapidAPI-Key": process.env.API_KEY,
|
'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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
// new method
|
|
||||||
app.get("/league/:league/:season", getLeagueTable);
|
|
||||||
|
|
||||||
// Current Premier League Table
|
app.get('/pl', async (req, res) => {
|
||||||
app.get("/pl", getLeagueTable);
|
const league = '39';
|
||||||
|
const year = '2021';
|
||||||
|
const query = league + year;
|
||||||
|
try {
|
||||||
|
client.get(query, async (err, leagueTable) => {
|
||||||
|
if (err) throw err;
|
||||||
|
|
||||||
app.get(
|
// Return cached league table if present
|
||||||
"/v2/:league/:year",
|
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
|
// 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
|
// Year must be no earlier than 2018
|
||||||
param("year").isIn([
|
param('year').isIn(['2018', '2019', '2020', '2021']),
|
||||||
"2018",
|
|
||||||
"2019",
|
|
||||||
"2020",
|
|
||||||
"2021",
|
|
||||||
"2022",
|
|
||||||
"2023",
|
|
||||||
"2024",
|
|
||||||
]),
|
|
||||||
],
|
],
|
||||||
async (req, res) => {
|
async (req, res) => {
|
||||||
// Validate
|
// Validate
|
||||||
@ -124,13 +124,13 @@ app.get(
|
|||||||
if (leagueTable) {
|
if (leagueTable) {
|
||||||
res.status(200).send({
|
res.status(200).send({
|
||||||
table: JSON.parse(leagueTable),
|
table: JSON.parse(leagueTable),
|
||||||
message: "data retrieved from cache",
|
message: 'data retrieved from cache',
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Fetch from the API
|
// Fetch from the API
|
||||||
reqSeason = new LeagueSeason(league, year);
|
reqSeason = new LeagueSeason(league, year);
|
||||||
const leagueTable = await axios.get(
|
const leagueTable = await axios.get(
|
||||||
"https://v3.football.api-sports.io/standings",
|
'https://v3.football.api-sports.io/standings',
|
||||||
reqSeason
|
reqSeason
|
||||||
);
|
);
|
||||||
// Save result to cache
|
// Save result to cache
|
||||||
@ -138,15 +138,14 @@ app.get(
|
|||||||
// Return data from API
|
// Return data from API
|
||||||
res.status(200).send({
|
res.status(200).send({
|
||||||
table: leagueTable.data,
|
table: leagueTable.data,
|
||||||
message: "cache miss",
|
message: 'cache miss',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res.status(500).send({ message: err.message });
|
res.status(500).send({ message: err.message });
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
app.listen(process.env.PORT || 3001, () => {
|
app.listen(process.env.PORT || 3001, () => {
|
||||||
console.log(`Server running`);
|
console.log(`Server running`);
|
||||||
|
1834
package-lock.json
generated
1834
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",
|
"name": "league_table",
|
||||||
"version": "0.5.0",
|
"version": "0.2.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": "20.x"
|
"node": "18.x"
|
||||||
},
|
},
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
@ -13,15 +13,14 @@
|
|||||||
"author": "nplayfair",
|
"author": "nplayfair",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"dotenv": "^16.5.0",
|
"dotenv": "^16.3.1",
|
||||||
"eslint-import-resolver-node": "^0.3.9",
|
"nodemon": "^3.0.2"
|
||||||
"nodemon": "^3.1.10"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.9.0",
|
"axios": "^1.6.2",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^5.1.0",
|
"express": "^4.18.2",
|
||||||
"express-validator": "^7.2.1",
|
"express-validator": "^7.0.1",
|
||||||
"redis": "^5.5.5"
|
"redis": "^4.6.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user