v2
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getSettings } from '@/lib/settings';
|
||||
import { logApiRequest } from '@/lib/api-logger';
|
||||
|
||||
// Revalidation times per fixture type
|
||||
const REVALIDATE: Record<string, number> = {
|
||||
live: 30, // live scores — refresh every 30s
|
||||
last: 300, // recent results — refresh every 5 min
|
||||
next: 300, // upcoming — refresh every 5 min
|
||||
};
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const startTime = Date.now();
|
||||
const { searchParams } = new URL(request.url);
|
||||
const type = searchParams.get('type') || 'live';
|
||||
|
||||
try {
|
||||
const settings = await getSettings();
|
||||
const apiKey = settings.apiKey || process.env.API_FOOTBALL_API_KEY;
|
||||
|
||||
if (!apiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'API key not configured' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
const params = new URLSearchParams({
|
||||
league: settings.leagueId.toString(),
|
||||
season: settings.season.toString(),
|
||||
});
|
||||
|
||||
if (type === 'live') {
|
||||
params.set('live', 'all');
|
||||
} else if (type === 'last') {
|
||||
params.set('last', '10');
|
||||
} else if (type === 'next') {
|
||||
params.set('next', '10');
|
||||
}
|
||||
|
||||
const revalidate = REVALIDATE[type] ?? 60;
|
||||
|
||||
const response = await fetch(
|
||||
`https://v3.football.api-sports.io/fixtures?${params.toString()}`,
|
||||
{
|
||||
headers: {
|
||||
'x-rapidapi-key': apiKey,
|
||||
'x-rapidapi-host': 'v3.football.api-sports.io',
|
||||
},
|
||||
next: { revalidate },
|
||||
}
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
// Fire-and-forget log (non-blocking)
|
||||
logApiRequest(`/fixtures?type=${type}`, response.status, duration);
|
||||
|
||||
return NextResponse.json(data, {
|
||||
headers: {
|
||||
'Cache-Control': `public, s-maxage=${revalidate}, stale-while-revalidate=${revalidate * 2}`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
const duration = Date.now() - startTime;
|
||||
logApiRequest(`/fixtures?type=${type}`, 500, duration);
|
||||
console.error('Fixtures API error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch fixtures' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user