mc-dashboard/app/api/logs/route.ts

34 lines
852 B
TypeScript
Raw Permalink Normal View History

import { NextRequest, NextResponse } from "next/server";
import { execSync } from "child_process";
import { auth } from "@/lib/auth";
import { memo } from "@/lib/cache";
export const dynamic = "force-dynamic";
export async function GET(req: NextRequest) {
const session = await auth();
if (!session) {
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
}
const lines = Math.min(
parseInt(req.nextUrl.searchParams.get("lines") || "50"),
200
);
try {
const logs = memo(`logs:${lines}`, 2000, () =>
execSync(`sudo journalctl -u minecraft.service --no-pager -n ${lines}`, {
encoding: "utf8",
timeout: 5000,
})
);
return NextResponse.json({ logs });
} catch (e) {
return NextResponse.json(
{ error: (e as Error).message },
{ status: 500 }
);
}
}