Persist Plutonium keys by server ID and add key management
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
// `g b` betas, `g s` scripts. Ignored while typing in inputs.
|
||||
const shortcuts: Record<string, string> = {
|
||||
f: '/', d: '/deployments', b: '/betas',
|
||||
s: '/scripts', c: '/configs', p: '/plugins'
|
||||
k: '/server-keys', s: '/scripts', c: '/configs', p: '/plugins'
|
||||
};
|
||||
let armed = false;
|
||||
let armTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
@@ -65,6 +65,7 @@
|
||||
{ href: '/configs', label: 'Configs', match: (p: string) => p.startsWith('/configs') },
|
||||
{ href: '/plugins', label: 'Plugins', match: (p: string) => p.startsWith('/plugins') },
|
||||
{ href: '/deployments', label: 'Deployments', match: (p: string) => p.startsWith('/deployments') },
|
||||
{ href: '/server-keys', label: 'Server keys', match: (p: string) => p.startsWith('/server-keys') },
|
||||
{ href: '/betas', label: 'Betas', match: (p: string) => p.startsWith('/betas') }
|
||||
];
|
||||
</script>
|
||||
|
||||
@@ -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) });
|
||||
}
|
||||
@@ -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 });
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import Button from '$lib/ui/components/primitives/Button.svelte';
|
||||
import Field from '$lib/ui/components/primitives/Field.svelte';
|
||||
import Input from '$lib/ui/components/primitives/Input.svelte';
|
||||
|
||||
interface SavedKey { server_id: string; deployed: number; updated_at: string }
|
||||
let keys = $state<SavedKey[]>([]);
|
||||
let serverId = $state('');
|
||||
let plutoKey = $state('');
|
||||
let editing = $state(false);
|
||||
let busy = $state(false);
|
||||
let loading = $state(true);
|
||||
let error = $state('');
|
||||
let notice = $state('');
|
||||
|
||||
async function load() {
|
||||
const res = await fetch('/api/server-keys');
|
||||
if (!res.ok) throw new Error('Could not load server keys');
|
||||
keys = (await res.json()).keys;
|
||||
}
|
||||
onMount(() => { load().catch(e => error = e.message).finally(() => loading = false); });
|
||||
function reset() { serverId = ''; plutoKey = ''; editing = false; }
|
||||
async function save(e: SubmitEvent) {
|
||||
e.preventDefault();
|
||||
busy = true; error = ''; notice = '';
|
||||
try {
|
||||
const res = await fetch('/api/server-keys', {
|
||||
method: 'PUT', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ serverId: serverId.trim(), plutoKey })
|
||||
});
|
||||
const body = await res.json();
|
||||
if (!res.ok) throw new Error(body.error ?? 'Could not save key');
|
||||
reset(); notice = 'Server key saved.'; await load();
|
||||
} catch (e) { error = (e as Error).message; }
|
||||
finally { busy = false; }
|
||||
}
|
||||
async function remove(id: string) {
|
||||
if (!confirm(`Remove the saved key for ${id}? Running servers are unaffected.`)) return;
|
||||
busy = true; error = ''; notice = '';
|
||||
try {
|
||||
const res = await fetch('/api/server-keys', {
|
||||
method: 'DELETE', headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ serverId: id })
|
||||
});
|
||||
if (!res.ok) throw new Error('Could not remove key');
|
||||
if (serverId === id) reset();
|
||||
notice = 'Saved key removed.'; await load();
|
||||
} catch (e) { error = (e as Error).message; }
|
||||
finally { busy = false; }
|
||||
}
|
||||
</script>
|
||||
|
||||
<svelte:head><title>Server keys · Electric Cherry</title></svelte:head>
|
||||
<h1>Server keys</h1>
|
||||
<p>Save a Plutonium key for each server ID, even when the server is not deployed. Destroying a server keeps its saved key.</p>
|
||||
<form onsubmit={save}>
|
||||
<h2>{editing ? 'Update key' : 'Add key'}</h2>
|
||||
<Field label="Server ID" hint="Use a map slug and lobby number from 1 to 20, e.g. t5_zombie_theater_2.">
|
||||
<Input bind:value={serverId} placeholder="t5_zombie_theater_2" required readonly={editing} disabled={busy} mono />
|
||||
</Field>
|
||||
<Field label="Plutonium server key" hint="Changes apply the next time this server ID is deployed.">
|
||||
<Input type="password" bind:value={plutoKey} placeholder="Paste key" autocomplete="off" required disabled={busy} />
|
||||
</Field>
|
||||
<div class="actions">
|
||||
<Button type="submit" variant="primary" disabled={busy || loading} loading={busy}>{editing ? 'Update key' : 'Save key'}</Button>
|
||||
{#if editing}<Button onclick={reset} disabled={busy}>Cancel</Button>{/if}
|
||||
</div>
|
||||
</form>
|
||||
{#if error}<p role="alert" class="error">{error}</p>{/if}
|
||||
{#if notice}<p role="status">{notice}</p>{/if}
|
||||
<h2>Saved keys</h2>
|
||||
{#if loading}<p>Loading keys…</p>
|
||||
{:else if keys.length === 0}<p>No saved keys yet. Add one above or save one when deploying a server.</p>
|
||||
{:else}
|
||||
<div class="table-wrap"><table>
|
||||
<thead><tr><th>Server ID</th><th>Status</th><th>Key</th><th>Actions</th></tr></thead>
|
||||
<tbody>{#each keys as key (key.server_id)}
|
||||
<tr>
|
||||
<td><code>{key.server_id}</code></td>
|
||||
<td>{key.deployed ? 'Deployed' : 'Not deployed'}</td>
|
||||
<td aria-label="Key saved">••••••••</td>
|
||||
<td><div class="actions">
|
||||
<Button disabled={busy} onclick={() => { serverId = key.server_id; plutoKey = ''; editing = true; error = ''; notice = ''; }}>Update</Button>
|
||||
<Button disabled={busy} onclick={() => remove(key.server_id)}>Remove</Button>
|
||||
</div></td>
|
||||
</tr>
|
||||
{/each}</tbody>
|
||||
</table></div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
h1 { margin-top: 0; }
|
||||
h2 { font-size: var(--text-md); }
|
||||
p { color: var(--text-secondary); }
|
||||
form { display: grid; gap: var(--space-4); max-width: 620px; padding: var(--space-6); background: var(--surface); border: 1px solid var(--border-subtle); border-radius: var(--radius-lg); margin: var(--space-6) 0; }
|
||||
form h2 { margin: 0; }
|
||||
.actions { display: flex; gap: var(--space-2); }
|
||||
.table-wrap { overflow-x: auto; }
|
||||
table { width: 100%; border-collapse: collapse; text-align: left; }
|
||||
th, td { padding: var(--space-4); border-bottom: 1px solid var(--border-subtle); }
|
||||
th { color: var(--text-tertiary); font-size: var(--text-sm); }
|
||||
code { font-family: var(--font-mono); }
|
||||
.error { color: var(--state-danger); }
|
||||
</style>
|
||||
Reference in New Issue
Block a user