export default async function server(request: Request): Promise<Response> {
try {
const eclipses = [
{ date: '2024-03-25T00:00:00Z', kind: 'lunar', type: 'penumbral', place: 'North and South America' },
{ date: '2024-04-08T00:00:00Z', kind: 'solar', type: 'total', place: 'partial: North and Central America; total: northern Mexico, central U.S., and eastern Canada' },
{ date: '2024-09-18T00:00:00Z', kind: 'lunar', type: 'partial', place: 'North and South America, Europe, and Africa' },
{ date: '2024-10-02T00:00:00Z', kind: 'solar', type: 'annular', place: 'partial: South Pacific and South America; annular: southern Chile and Argentina' },
{ date: '2025-03-14T00:00:00Z', kind: 'lunar', type: 'total', place: 'North and South America, Europe, and Africa' },
{ date: '2025-03-29T00:00:00Z', kind: 'solar', type: 'partial', place: 'northwestern Africa, Europe, and northwestern Russia' },
{ date: '2025-09-07T00:00:00Z', kind: 'lunar', type: 'total', place: 'Europe, Africa, Asia, and Australia' },
{ date: '2025-09-21T00:00:00Z', kind: 'solar', type: 'partial', place: 'New Zealand and Antarctica' },
{ date: '2026-02-17T00:00:00Z', kind: 'solar', type: 'annular', place: 'partial: southern Chile and Argentina, Antarctica, and southern Africa; annular: Antarctica' },
{ date: '2026-03-03T00:00:00Z', kind: 'lunar', type: 'total', place: 'Asia, Australia, and North and South America' },
{ date: '2026-08-12T00:00:00Z', kind: 'solar', type: 'total', place: 'partial: Alaska, Canada, and Greenland; total: eastern Greenland' },
{ date: '2026-08-28T00:00:00Z', kind: 'lunar', type: 'partial', place: 'North and South America, Europe, and Africa' }
];
const now = new Date();
const ongoingEclipse = eclipses.find((eclipse) => {
const eclipseDate = new Date(eclipse.date);
const eclipseEndDate = new Date(eclipseDate.getTime() + 21600000);
return now >= eclipseDate && now <= eclipseEndDate;
});
const result = {
isEclipseHappening: !!ongoingEclipse,
eclipseDetails: ongoingEclipse || null,
checkedAt: now.toISOString(),
};
return new Response(JSON.stringify(result, null, 2), {
headers: {
'Content-Type': 'application/json',
},
});
} catch (error) {
console.error('Error:', error);
return new Response(JSON.stringify({
error: 'An error occurred while checking for eclipses',
details: error.message
}, null, 2), {
status: 500,
headers: {
'Content-Type': 'application/json',
},
});
}
}