This commit is contained in:
2026-04-29 01:27:40 +03:30
parent 2ac51fe082
commit 5c548bc6db
4 changed files with 703 additions and 373 deletions
@@ -0,0 +1,71 @@
import { NextRequest, NextResponse } from "next/server";
const resolveBackendBaseUrl = (): string => {
const publicApiUrl = process.env.NEXT_PUBLIC_API_URL;
const serverApiUrl = process.env.ENVOY_GATEWAY_URL;
return (serverApiUrl || publicApiUrl || "http://node.crop-logic.ir").replace(/\/$/, "");
};
const resolveSensorExternalApiKey = (request: NextRequest): string | null => {
const requestApiKey = request.headers.get("x-api-key");
if (requestApiKey) {
return requestApiKey;
}
return (
process.env.SENSOR_EXTERNAL_API_KEY ||
process.env.SENSOR_EXTERNAL_API_LOGS_KEY ||
process.env.NEXT_PUBLIC_SENSOR_EXTERNAL_API_KEY ||
"12345"
);
};
export async function GET(request: NextRequest) {
const apiKey = resolveSensorExternalApiKey(request);
const authorization = request.headers.get("authorization");
if (!apiKey) {
return NextResponse.json(
{
detail:
"Sensor external API key is not configured on the frontend server.",
},
{ status: 500 },
);
}
const query = request.nextUrl.searchParams.toString();
const endpoint = `${resolveBackendBaseUrl()}/api/sensor-external-api/logs/${
query ? `?${query}` : ""
}`;
try {
const response = await fetch(endpoint, {
method: "GET",
headers: {
Accept: "application/json",
...(authorization ? { Authorization: authorization } : {}),
"X-API-Key": apiKey,
},
cache: "no-store",
});
const body = await response.text();
return new NextResponse(body, {
status: response.status,
headers: {
"content-type": response.headers.get("content-type") || "application/json",
},
});
} catch {
return NextResponse.json(
{
detail: "Failed to fetch sensor external logs from backend.",
},
{ status: 502 },
);
}
}