Elffuss BraidHub

kikocis/osiris public

master · 1.5 KiBRawPermalink
1
2import { NextResponse } from 'next/server';
3
4/**
5 * OSIRIS — Earthquake Data API
6 * Fetches real-time seismic events from USGS (last 24h, M2.5+)
7 * No API key required
8 */
9
10export async function GET() {
11 try {
12 const url = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson';
13 const res = await fetch(url, {
14 signal: AbortSignal.timeout(10000),
15 });
16
17 if (!res.ok) {
18 return NextResponse.json({ earthquakes: [], error: 'USGS unavailable' });
19 }
20
21 const data = await res.json();
22 const features = data.features || [];
23
24 const earthquakes = features.map((f: any) => {
25 const coords = f.geometry?.coordinates || [0, 0, 0];
26 const props = f.properties || {};
27 return {
28 id: f.id,
29 lat: coords[1],
30 lng: coords[0],
31 depth: coords[2],
32 magnitude: props.mag,
33 place: props.place,
34 time: props.time,
35 url: props.url,
36 tsunami: props.tsunami,
37 type: props.type,
38 felt: props.felt,
39 alert: props.alert,
40 };
41 });
42
43 return NextResponse.json({
44 earthquakes,
45 total: earthquakes.length,
46 timestamp: new Date().toISOString(),
47 }, {
48 headers: {
49 'Cache-Control': 'public, s-maxage=60, stale-while-revalidate=120',
50 },
51 });
52 } catch (error) {
53 console.error('Earthquake fetch error:', error);
54 return NextResponse.json({ earthquakes: [], error: 'Failed to fetch earthquake data' }, { status: 500 });
55 }
56}
57