pre-bemis
This commit is contained in:
45
app/api/debug/logs/route.ts
Normal file
45
app/api/debug/logs/route.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import fs from "fs";
|
||||
import { getLogPath } from "@/lib/logger";
|
||||
|
||||
const MAX_LINES = 100;
|
||||
|
||||
/**
|
||||
* GET /api/debug/logs?key=YOUR_DEBUG_LOGS_KEY
|
||||
*
|
||||
* Returns the last MAX_LINES from the app log file. Set DEBUG_LOGS_KEY in .env
|
||||
* and call with ?key=... to view. If DEBUG_LOGS_KEY is unset, returns 401.
|
||||
*/
|
||||
export async function GET(req: NextRequest) {
|
||||
const key = req.nextUrl.searchParams.get("key");
|
||||
const secret = process.env.DEBUG_LOGS_KEY;
|
||||
|
||||
if (!secret || key !== secret) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
const logPath = getLogPath();
|
||||
try {
|
||||
const raw = fs.readFileSync(logPath, "utf8");
|
||||
const lines = raw.split("\n").filter(Boolean);
|
||||
const recent = lines.slice(-MAX_LINES);
|
||||
return NextResponse.json({
|
||||
logPath,
|
||||
lines: recent.length,
|
||||
entries: recent.map((line) => {
|
||||
try {
|
||||
return JSON.parse(line) as Record<string, unknown>;
|
||||
} catch {
|
||||
return { raw: line };
|
||||
}
|
||||
}),
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to read log file", detail: message, logPath },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
31
app/api/debug/perf/route.ts
Normal file
31
app/api/debug/perf/route.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { NextRequest } from "next/server";
|
||||
import { NextResponse } from "next/server";
|
||||
import { logLine } from "@/lib/logger";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
type PerfPayload = {
|
||||
event?: string;
|
||||
data?: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = (await req.json()) as PerfPayload;
|
||||
const type = typeof body?.event === "string" ? body.event : "nav";
|
||||
const data = body?.data && typeof body.data === "object" ? body.data : {};
|
||||
const userAgent = req.headers.get("user-agent") ?? "";
|
||||
|
||||
logLine("perf.client", {
|
||||
type,
|
||||
userAgent,
|
||||
...data,
|
||||
});
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
logLine("perf.client.error", { message });
|
||||
return NextResponse.json({ ok: false, error: "Bad payload" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user