Persist Plutonium keys by server ID and add key management

This commit is contained in:
2026-09-17 15:54:10 -04:00
parent e4505a3be8
commit ae173517b6
10 changed files with 305 additions and 19 deletions
+22
View File
@@ -0,0 +1,22 @@
import { json } from '@sveltejs/kit';
import { listServerKeys, saveServerKey, removeServerKey, validServerId } from '$lib/server/fleet/server-keys.js';
export function GET() {
return json({ keys: listServerKeys() }, { headers: { 'Cache-Control': 'no-store' } });
}
export async function PUT({ request }: { request: Request }) {
const body = await request.json().catch(() => null);
try {
saveServerKey(body?.serverId, body?.plutoKey);
return json({ ok: true });
} catch (err) {
return json({ ok: false, error: (err as Error).message }, { status: 400 });
}
}
export async function DELETE({ request }: { request: Request }) {
const body = await request.json().catch(() => null);
if (!validServerId(body?.serverId)) return json({ error: 'Invalid server ID' }, { status: 400 });
return json({ ok: removeServerKey(body.serverId) });
}
+5 -7
View File
@@ -43,25 +43,23 @@ export async function POST({ request }: { request: Request }) {
gameMode?: string;
mapSlug?: string;
plutoKey?: string;
lobby?: number;
};
if (typeof body.gameMode !== 'string' || typeof body.mapSlug !== 'string' || typeof body.plutoKey !== 'string') {
if (!body || typeof body.gameMode !== 'string' || typeof body.mapSlug !== 'string' || (body.plutoKey !== undefined && typeof body.plutoKey !== 'string') || (body.lobby !== undefined && typeof body.lobby !== 'number')) {
return json(
{ ok: false, error: 'gameMode, mapSlug, and plutoKey are required strings', kind: 'bad-request' },
{ ok: false, error: 'gameMode and mapSlug are required strings; plutoKey and lobby are optional', kind: 'bad-request' },
{ status: 400 }
);
}
if (body.plutoKey.trim().length === 0) {
return json({ ok: false, error: 'plutoKey cannot be empty', kind: 'bad-request' }, { status: 400 });
}
const result = await createServer({
gameMode: body.gameMode as GameMode,
mapSlug: body.mapSlug,
plutoKey: body.plutoKey,
lobby: body.lobby,
createdBy: 'ui'
});
if (!result.ok) {
const status = result.kind === 'unconfigured' ? 503
const status = result.kind === 'bad-request' ? 400 : result.kind === 'unconfigured' ? 503
: result.kind === 'unknown-map' || result.kind === 'no-slot' || result.kind === 'no-port' ? 409
: 500;
return json({ ok: false, error: result.message, kind: result.kind }, { status });