SSE events bridge, PWA service worker, offline banner, lazy admin tabs
- New /api/events SSE endpoint (authed): pushes status every 5s and chat
on log-file mtime change (~1.5s poll). Heartbeat every 15s, hard-caps
each stream at 10min so the browser gets a clean auth refresh on
reconnect. Auto-aborts on client disconnect.
- Factored shared helpers out of the existing routes:
- lib/server-status.ts (probeStatus, reused by /api/status + SSE)
- lib/chat-log.ts (parseLogLine, readChatMessages, logMtime, reused by
/api/chat + SSE)
- EventsBridge client (mounted in Providers) opens one EventSource per
authed session and writes live data into the TanStack Query cache for
["status"] and ["chat"] — no refactor needed in consuming components,
they keep reading their usual query keys.
- Now that SSE pushes updates, polling intervals bumped: StatusCard and
ServerControls 10s -> 60s, ChatBridge 5s -> 30s. SSE handles realtime,
polling is safety fallback.
- OfflineBanner: sticky amber bar when navigator.onLine flips false.
- PWA: minimal public/sw.js with shell + asset cache (network-first for
HTML, stale-while-revalidate for static assets, never touches /api/*
or text/event-stream). ServiceWorkerRegister client registers it in
production only.
- AdminTabs now uses next/dynamic with skeleton fallbacks for Players /
Chat / Mods / Backups / Logs, keeping initial /admin bundle smaller.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
19d66c2de6
commit
359a12ef9d
14 changed files with 460 additions and 132 deletions
|
|
@ -1,92 +1,20 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { readFileSync, existsSync } from "fs";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { sendCommand } from "@/lib/rcon";
|
||||
import { readChatMessages } from "@/lib/chat-log";
|
||||
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
const LOG_FILE = "/home/minecraft/server/logs/latest.log";
|
||||
|
||||
type ChatMessage = {
|
||||
time: string;
|
||||
type: "chat" | "join" | "leave" | "death" | "server";
|
||||
player: string;
|
||||
message: string;
|
||||
};
|
||||
|
||||
function parseLogLine(line: string): ChatMessage | null {
|
||||
// [HH:MM:SS] [Server thread/INFO] [minecraft/DedicatedServer]: <Player> message
|
||||
const chatMatch = line.match(
|
||||
/\[(\d{2}:\d{2}:\d{2})\].*\[minecraft\/(?:DedicatedServer|MinecraftServer)\]:\s*<(\w+)>\s*(.*)/
|
||||
);
|
||||
if (chatMatch) {
|
||||
return { time: chatMatch[1], type: "chat", player: chatMatch[2], message: chatMatch[3] };
|
||||
}
|
||||
|
||||
// Player joins
|
||||
const joinMatch = line.match(
|
||||
/\[(\d{2}:\d{2}:\d{2})\].*\[minecraft\/(?:PlayerList|ServerPlayer)\]:\s*(\w+)\s+joined the game/
|
||||
);
|
||||
if (joinMatch) {
|
||||
return { time: joinMatch[1], type: "join", player: joinMatch[2], message: "joined the game" };
|
||||
}
|
||||
|
||||
// Player leaves
|
||||
const leaveMatch = line.match(
|
||||
/\[(\d{2}:\d{2}:\d{2})\].*\[minecraft\/(?:PlayerList|ServerPlayer)\]:\s*(\w+)\s+left the game/
|
||||
);
|
||||
if (leaveMatch) {
|
||||
return { time: leaveMatch[1], type: "leave", player: leaveMatch[2], message: "left the game" };
|
||||
}
|
||||
|
||||
// Deaths
|
||||
const deathMatch = line.match(
|
||||
/\[(\d{2}:\d{2}:\d{2})\].*\[minecraft\/(?:DedicatedServer|MinecraftServer)\]:\s*(\w+)\s+(was |died|drowned|burned|fell|starved|suffocated|hit|blew|withered|tried|experienced|went|walked|froze|was prick|was stung|was impaled|was squashed|was skewered|was squished|was pummeled|discovered)(.*)/
|
||||
);
|
||||
if (deathMatch) {
|
||||
return {
|
||||
time: deathMatch[1],
|
||||
type: "death",
|
||||
player: deathMatch[2],
|
||||
message: deathMatch[3] + (deathMatch[4] || ""),
|
||||
};
|
||||
}
|
||||
|
||||
// Server say command
|
||||
const sayMatch = line.match(
|
||||
/\[(\d{2}:\d{2}:\d{2})\].*\[minecraft\/(?:DedicatedServer|MinecraftServer)\]:\s*\[Server\]\s*(.*)/
|
||||
);
|
||||
if (sayMatch) {
|
||||
return { time: sayMatch[1], type: "server", player: "Server", message: sayMatch[2] };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const session = await auth();
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 403 });
|
||||
}
|
||||
|
||||
if (!existsSync(LOG_FILE)) {
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
|
||||
const maxLines = parseInt(req.nextUrl.searchParams.get("lines") || "100");
|
||||
|
||||
try {
|
||||
const content = readFileSync(LOG_FILE, "utf8");
|
||||
const lines = content.split("\n");
|
||||
const messages: ChatMessage[] = [];
|
||||
|
||||
// Parse from the end, collect up to maxLines relevant messages
|
||||
for (let i = lines.length - 1; i >= 0 && messages.length < maxLines; i--) {
|
||||
const msg = parseLogLine(lines[i]);
|
||||
if (msg) messages.unshift(msg);
|
||||
}
|
||||
|
||||
return NextResponse.json(messages);
|
||||
return NextResponse.json(readChatMessages(maxLines));
|
||||
} catch (e) {
|
||||
return NextResponse.json(
|
||||
{ error: (e as Error).message },
|
||||
|
|
@ -106,7 +34,6 @@ export async function POST(req: NextRequest) {
|
|||
return NextResponse.json({ error: "Invalid message" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Sanitize: strip newlines/carriage returns to prevent RCON command injection
|
||||
const sanitized = message.replace(/[\r\n]/g, "").trim();
|
||||
if (!sanitized) {
|
||||
return NextResponse.json({ error: "Empty message" }, { status: 400 });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue