File size: 3,435 Bytes
df1e116 9fa2584 df1e116 cef360a df1e116 9fa2584 df1e116 9fa2584 df1e116 | 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | /**
* Server-side API proxy route.
* Forwards all requests to the backend — eliminates CORS since
* the browser only talks to the Next.js server (same-origin).
*
* Supports GET, POST, and DELETE methods.
* Passes through Authorization headers for authenticated requests.
*
* Backend URL is read from BACKEND_URL env var (server-side only).
*/
const BACKEND_URL = process.env.BACKEND_URL;
export async function GET(request, { params }) {
const { path } = await params;
const pathStr = path.join('/');
const { searchParams } = new URL(request.url);
const query = searchParams.toString();
const url = `${BACKEND_URL}/${pathStr}${query ? `?${query}` : ''}`;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 20000); // 20s
try {
const response = await fetch(url, {
signal: controller.signal,
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
...(request.headers.get('authorization')
? { Authorization: request.headers.get('authorization') }
: {}),
},
// Only cache data endpoints (tourist info), NOT user-specific endpoints like /sessions
...(pathStr.startsWith('data/')
? { next: { revalidate: 3600 } } // Cache tourist data for 1 hour
: { cache: 'no-store' } // Never cache session/chat/auth endpoints
),
});
clearTimeout(timeout);
const data = await response.json();
return Response.json(data, { status: response.status });
} catch (err) {
clearTimeout(timeout);
console.error('Proxy fetch error:', err);
return Response.json(
{ error: 'Backend request timeout or failed' },
{ status: 504 }
);
}
}
export async function POST(request, { params }) {
const { path } = await params;
const pathStr = path.join('/');
const url = `${BACKEND_URL}/${pathStr}`;
// Safely handle empty body (e.g. /auth sends no body)
let body = null;
try {
const text = await request.text();
body = text ? text : null;
} catch {
body = null;
}
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
...(request.headers.get('authorization')
? { Authorization: request.headers.get('authorization') }
: {}),
},
...(body ? { body } : {}),
});
const data = await response.json();
return Response.json(data, { status: response.status });
}
export async function DELETE(request, { params }) {
const { path } = await params;
const pathStr = path.join('/');
const url = `${BACKEND_URL}/${pathStr}`;
const response = await fetch(url, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'ngrok-skip-browser-warning': 'true',
...(request.headers.get('authorization')
? { Authorization: request.headers.get('authorization') }
: {}),
},
});
const data = await response.json();
return Response.json(data, { status: response.status });
}
|