20 lines
828 B
TypeScript
20 lines
828 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { fetchServerForecast, parseForecastCoordinate } from "@/lib/server-forecast";
|
|
|
|
export async function GET(request: Request) {
|
|
const { searchParams } = new URL(request.url);
|
|
const latitude = parseForecastCoordinate(searchParams.get("latitude"), -90, 90);
|
|
const longitude = parseForecastCoordinate(searchParams.get("longitude"), -180, 180);
|
|
if (latitude === null || longitude === null) {
|
|
return NextResponse.json({ error: "Invalid coordinates." }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
return NextResponse.json(await fetchServerForecast(latitude, longitude), {
|
|
headers: { "Cache-Control": "public, s-maxage=900, stale-while-revalidate=1800" },
|
|
});
|
|
} catch {
|
|
return NextResponse.json({ error: "Forecast service is unavailable." }, { status: 502 });
|
|
}
|
|
}
|