Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions forester/dashboard/src/app/api/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { NextResponse } from "next/server";

export const dynamic = "force-dynamic";

const BACKEND_URL =
process.env.FORESTER_API_URL || "http://127.0.0.1:8080";

const BACKEND_TIMEOUT_MS = Number(
process.env.FORESTER_API_TIMEOUT_MS ?? 8000
);

function isAbortError(error: unknown): boolean {
return (
typeof error === "object" &&
error !== null &&
"name" in error &&
(error as { name?: string }).name === "AbortError"
);
}

function joinBackendUrl(path: string): string {
const base = BACKEND_URL.replace(/\/+$/, "");
return `${base}/${path}`;
}

export async function GET(
_request: Request,
{ params }: { params: Promise<{ path: string[] }> }
) {
const { path } = await params;
const backendPath = path.join("/");
const upstream = joinBackendUrl(backendPath);

const timeoutMs =
Number.isFinite(BACKEND_TIMEOUT_MS) && BACKEND_TIMEOUT_MS > 0
? BACKEND_TIMEOUT_MS
: 8000;

const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);

try {
const response = await fetch(upstream, {
cache: "no-store",
signal: controller.signal,
});

const contentType = response.headers.get("content-type") ?? "";
const payload = contentType.includes("application/json")
? await response.json()
: { message: await response.text() };

if (!response.ok) {
return NextResponse.json(
{
error: `Forester backend returned ${response.status}`,
upstream,
details: payload,
},
{ status: response.status }
);
}

return NextResponse.json(payload, { status: response.status });
} catch (error) {
if (isAbortError(error)) {
return NextResponse.json(
{
error: `Backend request timed out after ${timeoutMs}ms`,
upstream,
},
{ status: 504 }
);
}

return NextResponse.json(
{ error: "Backend unavailable", upstream },
{ status: 502 }
);
} finally {
clearTimeout(timer);
}
}
8 changes: 7 additions & 1 deletion forester/dashboard/src/app/compressible/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export default function CompressiblePage() {

return (
<div className="space-y-4">
<h2 className="text-xl font-bold">Compressible Accounts</h2>
<div>
<h2 className="text-xl font-bold">Compressible Accounts</h2>
<p className="text-sm text-gray-500 mt-1">
Track what is currently compressible, what is waiting on rent
windows, and how fresh this view is.
</p>
</div>
<CompressiblePanel data={data} />
</div>
);
Expand Down
Loading