Server: - lib/cache.ts: tiny TTL memo for hot paths. - lib/rcon.ts: pooled connection with 30s idle close and one-shot retry; was opening a fresh TCP per RCON call. - /api/status: race protocol-ping + RCON with Promise.any (was sequential with 5s timeouts) and memo 3s; adds Cache-Control. - /api/players: memo 10s, invalidated on mutations. - /api/mods: getModDetails memo 10s (invalidated on install/remove) — eliminates per-request readdirSync/statSync/AdmZip parse. - /api/analytics: reverse-scan JSONL to window cutoff (O(window) vs O(file)) and memo 30s. - /api/logs: memo 2s to throttle journalctl spawns during Live mode. Client: - StatusCard + ServerControls: staleTime 5s, both poll every 10s, dedup via shared query key. - Analytics: staleTime 30s; memoize sparkline SVG paths. - Modrinth search icons + mc-heads avatars: width/height + loading=lazy + decoding=async. - next.config.ts: images.remotePatterns for future next/image migration, explicit compress=true. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { status } from "minecraft-server-util";
|
|
import { execSync } from "child_process";
|
|
import { MC_SERVER_IP, MC_SERVER_PORT } from "@/lib/constants";
|
|
import { sendCommand } from "@/lib/rcon";
|
|
import { memoAsync } from "@/lib/cache";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
type StatusResult = {
|
|
online: boolean;
|
|
starting?: boolean;
|
|
players: { online: number; max: number };
|
|
version?: string;
|
|
motd?: string;
|
|
};
|
|
|
|
async function probeStatus(): Promise<StatusResult> {
|
|
// Race protocol ping + RCON in parallel — whichever wins first signals "online"
|
|
const ping = status(MC_SERVER_IP, MC_SERVER_PORT, { timeout: 3000 }).then(
|
|
(r): StatusResult => ({
|
|
online: true,
|
|
players: { online: r.players.online, max: r.players.max },
|
|
version: r.version.name,
|
|
motd: r.motd.clean,
|
|
})
|
|
);
|
|
|
|
const rcon = sendCommand("list").then((response): StatusResult => {
|
|
const match = response.match(/There are (\d+) of a max of (\d+) players/);
|
|
return {
|
|
online: true,
|
|
players: {
|
|
online: match ? parseInt(match[1], 10) : 0,
|
|
max: match ? parseInt(match[2], 10) : 20,
|
|
},
|
|
};
|
|
});
|
|
|
|
try {
|
|
return await Promise.any([ping, rcon]);
|
|
} catch {
|
|
// Both failed — check if process is up
|
|
let starting = false;
|
|
try {
|
|
const out = execSync("systemctl is-active minecraft.service", {
|
|
encoding: "utf8",
|
|
}).trim();
|
|
starting = out === "active" || out === "activating";
|
|
} catch {}
|
|
return { online: false, starting, players: { online: 0, max: 0 } };
|
|
}
|
|
}
|
|
|
|
export async function GET() {
|
|
const result = await memoAsync("status", 3000, probeStatus);
|
|
return NextResponse.json(result, {
|
|
headers: { "Cache-Control": "public, max-age=3" },
|
|
});
|
|
}
|