Initial pocketkv: dashboard, read-only API, OpenAPI spec
A self-hosted key-value store for serving JSON values to static sites. - Dashboard behind HTTP Basic Auth (credentials from .env) to manage stores, edit JSON values in an Ace editor, and create read-only API keys - Read-only API authenticated with bearer tokens, CORS enabled - OpenAPI 3.1 spec served at /openapi.yaml - Go standard library only; data persisted to a single JSON file Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
commit
cb29d58aa2
16 files changed
+1318
No files matched your search
@@ -0,0 +1,23 @@
|
||||
{{define "content"}}
|
||||
<h1>Stores</h1>
|
||||
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
|
||||
<form method="post" action="/stores" class="row">
|
||||
<input type="text" name="name" placeholder="new-store-name" required autocomplete="off">
|
||||
<button class="primary" type="submit">Create store</button>
|
||||
</form>
|
||||
{{if .Stores}}
|
||||
<table>
|
||||
<tr><th>Name</th><th>Values</th><th>API keys</th><th>Created</th></tr>
|
||||
{{range .Stores}}
|
||||
<tr>
|
||||
<td><a href="/stores/{{.Name}}">{{.Name}}</a></td>
|
||||
<td>{{.Values}}</td>
|
||||
<td>{{.APIKeys}}</td>
|
||||
<td class="muted">{{.CreatedAt.Format "2006-01-02"}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
{{else}}
|
||||
<p class="muted">No stores yet.</p>
|
||||
{{end}}
|
||||
{{end}}
|
||||
@@ -0,0 +1,45 @@
|
||||
{{define "layout"}}<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{block "title" .}}pocketkv{{end}}</title>
|
||||
<style>
|
||||
:root { --bg:#fff; --fg:#1a1a1a; --muted:#6b6b6b; --line:#e3e3e3; --accent:#2f6fde; --danger:#c62828; --code:#f4f4f4; color-scheme: light dark; }
|
||||
@media (prefers-color-scheme: dark) { :root { --bg:#151515; --fg:#e8e8e8; --muted:#999; --line:#2c2c2c; --accent:#6ea0ff; --danger:#ef6461; --code:#1f1f1f; } }
|
||||
* { box-sizing: border-box; }
|
||||
body { margin:0; background:var(--bg); color:var(--fg); font:15px/1.5 system-ui, sans-serif; }
|
||||
header { display:flex; justify-content:space-between; align-items:center; padding:12px 20px; border-bottom:1px solid var(--line); }
|
||||
header .brand { font-weight:700; color:var(--fg); text-decoration:none; }
|
||||
main { max-width:960px; margin:0 auto; padding:20px; }
|
||||
a { color:var(--accent); }
|
||||
h1 { font-size:1.4rem; margin:0 0 16px; } h2 { font-size:1.1rem; margin:32px 0 12px; }
|
||||
table { width:100%; border-collapse:collapse; }
|
||||
th, td { text-align:left; padding:8px 6px; border-bottom:1px solid var(--line); vertical-align:middle; }
|
||||
th { color:var(--muted); font-weight:500; font-size:.85rem; }
|
||||
td.actions { text-align:right; white-space:nowrap; }
|
||||
code, pre { font-family:ui-monospace, SFMono-Regular, Menlo, monospace; font-size:.85rem; background:var(--code); border-radius:4px; }
|
||||
code { padding:1px 4px; } pre { padding:12px; overflow-x:auto; }
|
||||
.preview { color:var(--muted); max-width:420px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
|
||||
form.inline { display:inline; }
|
||||
.row { display:flex; gap:8px; align-items:center; flex-wrap:wrap; margin:12px 0; }
|
||||
input[type=text] { padding:7px 9px; border:1px solid var(--line); border-radius:6px; background:var(--bg); color:var(--fg); font:inherit; min-width:240px; }
|
||||
button, .button { padding:7px 14px; border:1px solid var(--line); border-radius:6px; background:var(--code); color:var(--fg); font:inherit; cursor:pointer; text-decoration:none; display:inline-block; }
|
||||
button.primary, .button.primary { background:var(--accent); border-color:var(--accent); color:#fff; }
|
||||
button.danger { color:var(--danger); background:none; border-color:transparent; padding:4px 8px; }
|
||||
.error { color:var(--danger); }
|
||||
.muted { color:var(--muted); }
|
||||
.tablist { display:flex; gap:2px; border-bottom:1px solid var(--line); }
|
||||
.tablist button { background:none; border:none; border-bottom:2px solid transparent; border-radius:0; padding:6px 12px; color:var(--muted); }
|
||||
.tablist button[aria-selected=true] { color:var(--fg); border-bottom-color:var(--accent); }
|
||||
.tablist .copy { margin-left:auto; }
|
||||
.tabs pre { margin-top:0; border-top-left-radius:0; border-top-right-radius:0; }
|
||||
.crumbs { color:var(--muted); margin:0 0 8px; }
|
||||
</style>
|
||||
{{block "head" .}}{{end}}
|
||||
</head>
|
||||
<body>
|
||||
<header><a class="brand" href="/">pocketkv</a><a href="/openapi.yaml">API spec</a></header>
|
||||
<main>{{template "content" .}}</main>
|
||||
</body>
|
||||
</html>{{end}}
|
||||
@@ -0,0 +1,137 @@
|
||||
{{define "title"}}{{.Store.Name}} · pocketkv{{end}}
|
||||
{{define "content"}}
|
||||
<p class="crumbs"><a href="/">Stores</a> /</p>
|
||||
<h1>{{.Store.Name}}</h1>
|
||||
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
|
||||
|
||||
<h2>Values</h2>
|
||||
<div class="row"><a class="button primary" href="/stores/{{.Store.Name}}/value">New value</a></div>
|
||||
{{if .Values}}
|
||||
<table>
|
||||
<tr><th>Key</th><th>Value</th><th>Size</th><th></th></tr>
|
||||
{{range .Values}}
|
||||
<tr>
|
||||
<td><a href="/stores/{{$.Store.Name}}/value?key={{.Key}}"><code>{{.Key}}</code></a></td>
|
||||
<td class="preview"><code>{{.Preview}}</code></td>
|
||||
<td class="muted">{{.Size}} B</td>
|
||||
<td class="actions">
|
||||
<form class="inline" method="post" action="/stores/{{$.Store.Name}}/value/delete" onsubmit="return confirm('Delete {{.Key}}?')">
|
||||
<input type="hidden" name="key" value="{{.Key}}">
|
||||
<button class="danger" type="submit">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
{{else}}
|
||||
<p class="muted">No values yet.</p>
|
||||
{{end}}
|
||||
|
||||
<h2 id="keys">Read-only API keys</h2>
|
||||
<form method="post" action="/stores/{{.Store.Name}}/keys" class="row">
|
||||
<input type="text" name="label" placeholder="label, e.g. my-blog" maxlength="64" autocomplete="off">
|
||||
<button type="submit">Create API key</button>
|
||||
</form>
|
||||
{{if .Store.APIKeys}}
|
||||
<table>
|
||||
<tr><th>Label</th><th>Key</th><th>Created</th><th></th></tr>
|
||||
{{range .Store.APIKeys}}
|
||||
<tr>
|
||||
<td>{{if .Label}}{{.Label}}{{else}}<span class="muted">—</span>{{end}}</td>
|
||||
<td><code>{{.Key}}</code> <button type="button" onclick="navigator.clipboard.writeText('{{.Key}}'); this.textContent='Copied'">Copy</button></td>
|
||||
<td class="muted">{{.CreatedAt.Format "2006-01-02"}}</td>
|
||||
<td class="actions">
|
||||
<form class="inline" method="post" action="/stores/{{$.Store.Name}}/keys/delete" onsubmit="return confirm('Revoke this key? Sites using it will stop working.')">
|
||||
<input type="hidden" name="key" value="{{.Key}}">
|
||||
<button class="danger" type="submit">Revoke</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
{{with index .Store.APIKeys 0}}
|
||||
<h2>Usage</h2>
|
||||
<div class="tabs" id="usage">
|
||||
<div class="tablist" role="tablist">
|
||||
<button type="button" role="tab" data-tab="js" aria-selected="true">JavaScript</button>
|
||||
<button type="button" role="tab" data-tab="html">HTML</button>
|
||||
<button type="button" role="tab" data-tab="curl">curl</button>
|
||||
<button type="button" role="tab" data-tab="python">Python</button>
|
||||
<button type="button" role="tab" data-tab="go">Go</button>
|
||||
<button type="button" class="copy">Copy</button>
|
||||
</div>
|
||||
<pre data-panel="js">const headers = { Authorization: "Bearer {{.Key}}" };
|
||||
|
||||
// one value
|
||||
const value = await fetch("{{$.BaseURL}}/api/v1/values/{{$.SampleKey}}", { headers }).then(r => r.json());
|
||||
|
||||
// every value in the store, as { key: value }
|
||||
const all = await fetch("{{$.BaseURL}}/api/v1/values", { headers }).then(r => r.json());</pre>
|
||||
<pre data-panel="html" hidden><pre id="value">loading…</pre>
|
||||
<script type="module">
|
||||
const res = await fetch("{{$.BaseURL}}/api/v1/values/{{$.SampleKey}}", {
|
||||
headers: { Authorization: "Bearer {{.Key}}" },
|
||||
});
|
||||
const value = await res.json();
|
||||
document.getElementById("value").textContent = JSON.stringify(value, null, 2);
|
||||
</script></pre>
|
||||
<pre data-panel="curl" hidden># one value
|
||||
curl -H "Authorization: Bearer {{.Key}}" \
|
||||
{{$.BaseURL}}/api/v1/values/{{$.SampleKey}}
|
||||
|
||||
# every value in the store
|
||||
curl -H "Authorization: Bearer {{.Key}}" \
|
||||
{{$.BaseURL}}/api/v1/values</pre>
|
||||
<pre data-panel="python" hidden>import json
|
||||
import urllib.request
|
||||
|
||||
req = urllib.request.Request(
|
||||
"{{$.BaseURL}}/api/v1/values/{{$.SampleKey}}",
|
||||
headers={"Authorization": "Bearer {{.Key}}"},
|
||||
)
|
||||
with urllib.request.urlopen(req) as res:
|
||||
value = json.load(res)</pre>
|
||||
<pre data-panel="go" hidden>req, _ := http.NewRequest("GET", "{{$.BaseURL}}/api/v1/values/{{$.SampleKey}}", nil)
|
||||
req.Header.Set("Authorization", "Bearer {{.Key}}")
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var value any
|
||||
if err := json.NewDecoder(res.Body).Decode(&value); err != nil {
|
||||
log.Fatal(err)
|
||||
}</pre>
|
||||
</div>
|
||||
<script>
|
||||
(() => {
|
||||
const root = document.getElementById("usage");
|
||||
const tabs = root.querySelectorAll("[data-tab]");
|
||||
const panels = root.querySelectorAll("[data-panel]");
|
||||
const show = (name) => {
|
||||
tabs.forEach(t => t.setAttribute("aria-selected", t.dataset.tab === name));
|
||||
panels.forEach(p => p.hidden = p.dataset.panel !== name);
|
||||
try { localStorage.setItem("pocketkv-usage-tab", name); } catch {}
|
||||
};
|
||||
tabs.forEach(t => t.onclick = () => show(t.dataset.tab));
|
||||
try { const saved = localStorage.getItem("pocketkv-usage-tab"); if (saved && root.querySelector(`[data-tab="${saved}"]`)) show(saved); } catch {}
|
||||
root.querySelector(".copy").onclick = (e) => {
|
||||
navigator.clipboard.writeText(root.querySelector("[data-panel]:not([hidden])").textContent);
|
||||
e.target.textContent = "Copied";
|
||||
setTimeout(() => e.target.textContent = "Copy", 1500);
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
<p class="muted">Full reference: <a href="/openapi.yaml">openapi.yaml</a></p>
|
||||
{{end}}
|
||||
{{else}}
|
||||
<p class="muted">No API keys yet. Create one to read this store from your sites.</p>
|
||||
{{end}}
|
||||
|
||||
<h2>Danger zone</h2>
|
||||
<form method="post" action="/stores/{{.Store.Name}}/delete" onsubmit="return confirm('Delete store {{.Store.Name}} with all its values and keys?')">
|
||||
<button class="danger" type="submit" style="border-color:currentColor">Delete store</button>
|
||||
</form>
|
||||
{{end}}
|
||||
@@ -0,0 +1,72 @@
|
||||
{{define "title"}}{{if .IsNew}}New value{{else}}{{.Key}}{{end}} · {{.Store}} · pocketkv{{end}}
|
||||
{{define "head"}}
|
||||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/src-min-noconflict/ace.js"></script>
|
||||
<style>
|
||||
#editor { height:65vh; border:1px solid var(--line); border-radius:6px; }
|
||||
#value { width:100%; height:65vh; font-family:ui-monospace, Menlo, monospace; font-size:14px; }
|
||||
</style>
|
||||
{{end}}
|
||||
{{define "content"}}
|
||||
<p class="crumbs"><a href="/">Stores</a> / <a href="/stores/{{.Store}}">{{.Store}}</a> /</p>
|
||||
<form method="post" action="/stores/{{.Store}}/value" id="form">
|
||||
{{if .IsNew}}
|
||||
<input type="hidden" name="new" value="1">
|
||||
<div class="row"><input type="text" name="key" value="{{.Key}}" placeholder="key, e.g. site.hero" required autofocus autocomplete="off"></div>
|
||||
{{else}}
|
||||
<h1><code>{{.Key}}</code></h1>
|
||||
<input type="hidden" name="key" value="{{.Key}}">
|
||||
{{end}}
|
||||
{{if .Error}}<p class="error">{{.Error}}</p>{{end}}
|
||||
<div id="editor" hidden></div>
|
||||
<textarea name="value" id="value" spellcheck="false">{{.Value}}</textarea>
|
||||
<div class="row">
|
||||
<button class="primary" type="submit">Save</button>
|
||||
<button type="button" id="format">Format</button>
|
||||
<span class="muted">⌘/Ctrl+S saves</span>
|
||||
<span id="status" class="error"></span>
|
||||
</div>
|
||||
</form>
|
||||
<script>
|
||||
(() => {
|
||||
const form = document.getElementById("form");
|
||||
const textarea = document.getElementById("value");
|
||||
const status = document.getElementById("status");
|
||||
let get = () => textarea.value, set = (v) => { textarea.value = v; };
|
||||
|
||||
// Use Ace when the CDN is reachable; otherwise the plain textarea still works.
|
||||
if (window.ace) {
|
||||
const el = document.getElementById("editor");
|
||||
el.hidden = false;
|
||||
textarea.hidden = true;
|
||||
ace.config.set("basePath", "https://cdn.jsdelivr.net/npm/[email protected]/src-min-noconflict/");
|
||||
const dark = matchMedia("(prefers-color-scheme: dark)").matches;
|
||||
const editor = ace.edit(el, {
|
||||
mode: "ace/mode/json",
|
||||
theme: dark ? "ace/theme/tomorrow_night" : "ace/theme/chrome",
|
||||
value: textarea.value,
|
||||
tabSize: 2, useSoftTabs: true, showPrintMargin: false, fontSize: 14,
|
||||
});
|
||||
editor.commands.addCommand({
|
||||
name: "save", bindKey: { win: "Ctrl-S", mac: "Command-S" },
|
||||
exec: () => form.requestSubmit(),
|
||||
});
|
||||
editor.focus();
|
||||
get = () => editor.getValue();
|
||||
set = (v) => editor.setValue(v, -1);
|
||||
}
|
||||
|
||||
const parse = () => {
|
||||
try { status.textContent = ""; return [JSON.parse(get()), true]; }
|
||||
catch (e) { status.textContent = "Invalid JSON: " + e.message; return [null, false]; }
|
||||
};
|
||||
document.getElementById("format").onclick = () => {
|
||||
const [v, ok] = parse();
|
||||
if (ok) set(JSON.stringify(v, null, 2));
|
||||
};
|
||||
form.addEventListener("submit", (e) => {
|
||||
if (!parse()[1]) { e.preventDefault(); return; }
|
||||
textarea.value = get();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
{{end}}
|
||||
Reference in new issue
Block a user