56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
// app/api/n8n/status/route.ts
|
|
import { NextResponse } from "next/server";
|
|
|
|
// Cache für 30 Sekunden, damit wir n8n nicht zuspammen
|
|
export const revalidate = 30;
|
|
|
|
export async function GET() {
|
|
try {
|
|
// Rufe den n8n Webhook auf
|
|
// Add timestamp to query to bypass Cloudflare cache
|
|
const res = await fetch(
|
|
`${process.env.N8N_WEBHOOK_URL}/webhook/denshooter-71242/status?t=${Date.now()}`,
|
|
{
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
next: { revalidate: 30 },
|
|
},
|
|
);
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`n8n error: ${res.status}`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
|
|
// n8n gibt oft ein Array zurück: [{...}]. Wir wollen nur das Objekt.
|
|
const statusData = Array.isArray(data) ? data[0] : data;
|
|
|
|
// Safety check: if statusData is still undefined/null (e.g. empty array), use fallback
|
|
if (!statusData) {
|
|
throw new Error("Empty data received from n8n");
|
|
}
|
|
|
|
// Ensure coding object has proper structure
|
|
if (statusData.coding && typeof statusData.coding === "object") {
|
|
// Already properly formatted from n8n
|
|
} else if (statusData.coding === null || statusData.coding === undefined) {
|
|
// No coding data - keep as null
|
|
statusData.coding = null;
|
|
}
|
|
|
|
return NextResponse.json(statusData);
|
|
} catch (error) {
|
|
console.error("Error fetching n8n status:", error);
|
|
// Leeres Fallback-Objekt, damit die Seite nicht abstürzt
|
|
return NextResponse.json({
|
|
status: { text: "offline", color: "gray" },
|
|
music: null,
|
|
gaming: null,
|
|
coding: null,
|
|
});
|
|
}
|
|
}
|