import { NextRequest, NextResponse } from "next/server"; import { exec } from "child_process"; import { auth } from "@/lib/auth"; import { restoreSnapshot } from "@/lib/snapshots"; import { rebuildModpack, waitForServer } from "@/lib/mods"; export async function POST(req: NextRequest) { const session = await auth(); if (!session) { return NextResponse.json({ error: "Unauthorized" }, { status: 403 }); } const { dirName } = await req.json(); if (!dirName || dirName.includes("/") || dirName.includes("..")) { return NextResponse.json({ error: "Invalid snapshot" }, { status: 400 }); } try { restoreSnapshot(dirName); await new Promise((resolve, reject) => { exec("sudo systemctl restart minecraft.service", (err) => { if (err) reject(err); else resolve(); }); }); const online = await waitForServer(90000); try { rebuildModpack(); } catch {} return NextResponse.json({ success: true, online, message: online ? "Snapshot restored. Server is online." : "Snapshot restored. Server is starting...", }); } catch (e) { return NextResponse.json( { success: false, message: (e as Error).message }, { status: 500 } ); } }