-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
82 lines (65 loc) · 2.85 KB
/
route.ts
File metadata and controls
82 lines (65 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// app/api/lambda/route.ts
import { NextRequest, NextResponse } from 'next/server';
const getDataFromApi = async (latitude: string, longitude: string) => {
// For server-side code, use process.env directly
const baseUrl = process.env.BACKEND_ORCHESTRATOR_API_URL;
if (!baseUrl) {
throw new Error('API URL not configured');
}
// Construct the full URL with query parameters
const apiUrl = `${baseUrl}?latitude=${latitude}&longitude=${longitude}`;
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
};
export async function GET(request: NextRequest) {
try {
const { searchParams } = new URL(request.url);
const latitude = searchParams.get('latitude');
const longitude = searchParams.get('longitude');
if (!latitude || !longitude) {
return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 });
}
// Call getDataFromApi and get the result directly
const result = await getDataFromApi(latitude, longitude);
return NextResponse.json(result);
} catch (error: any) {
console.error('Error invoking API:', error);
return NextResponse.json(
{ error: 'Internal Server Error', details: error.message },
{ status: 500 }
);
}
}
// // Call to mock data. Comment out the mockData you don't want to use
// import fs from 'fs';
// import { NextRequest, NextResponse } from 'next/server';
// import path from 'path';
// export async function GET(request: NextRequest) {
// try {
// const { searchParams } = new URL(request.url);
// const latitude = searchParams.get('latitude');
// const longitude = searchParams.get('longitude');
// if (!latitude || !longitude) {
// return NextResponse.json({ error: 'Missing required parameters' }, { status: 400 });
// }
// // Simulate some delay to mimic a real API call
// await new Promise(resolve => setTimeout(resolve, 100));
// // Read the JSON file
// // const filePath = path.join(process.cwd(), 'public', 'data', 'mockData1.json'); // mock dataset 1: one election, one contest/jurisdiction, WITH picture links
// const filePath = path.join(process.cwd(), 'public', 'data', 'mockData2.json'); // mock dataset 2: multiple elections, multiple jurisdictions
// const fileContents = fs.readFileSync(filePath, 'utf8');
// const data = JSON.parse(fileContents);
// // Log the received parameters (similar to logging the event in the original code)
// console.log('Mock API received parameters:', { latitude, longitude });
// return NextResponse.json(data);
// } catch (error: any) {
// console.error('Error in mock API:', error);
// return NextResponse.json(
// { error: 'Internal Server Error', details: error.message },
// { status: 500 }
// );
// }
// }