mirror of
https://github.com/djdevin/recflare.git
synced 2026-09-09 15:11:29 -07:00
remove kv IDs
This commit is contained in:
@@ -10,3 +10,9 @@ RECFLARE_DOMAIN=rec.example.com
|
|||||||
# Kept out of the committed wrangler.jsonc (which uses a "local" placeholder) and
|
# Kept out of the committed wrangler.jsonc (which uses a "local" placeholder) and
|
||||||
# spliced in at deploy time. Required to deploy any worker that uses D1.
|
# spliced in at deploy time. Required to deploy any worker that uses D1.
|
||||||
# RECFLARE_D1=d44083e1-5bfe-4467-aa9a-f13c5c2496d5
|
# RECFLARE_D1=d44083e1-5bfe-4467-aa9a-f13c5c2496d5
|
||||||
|
|
||||||
|
# KV namespace ids, as a compact JSON object keyed by binding name. Each namespace
|
||||||
|
# is distinct (create with `wrangler kv namespace create <BINDING>`). Kept out of
|
||||||
|
# the committed wrangler.jsonc (which uses "local" placeholders) and spliced in at
|
||||||
|
# deploy time. Required to deploy any worker with the matching KV binding.
|
||||||
|
# RECFLARE_KV={"RECFLARE_MATCH_PRESENCE":"9f53f04b7dd244658d59f515a14748b6","RECFLARE_PLAYER_SETTINGS":"d33a90014e904b0eac720bddcbe0b036"}
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
# Wrangler
|
# Wrangler
|
||||||
.wrangler
|
.wrangler
|
||||||
.dev.vars
|
.dev.vars
|
||||||
wrangler.generated.jsonc
|
wrangler.generated.*
|
||||||
|
|
||||||
# Astro generated types
|
# Astro generated types
|
||||||
.astro/
|
.astro/
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
"kv_namespaces": [
|
"kv_namespaces": [
|
||||||
{
|
{
|
||||||
"binding": "RECFLARE_MATCH_PRESENCE",
|
"binding": "RECFLARE_MATCH_PRESENCE",
|
||||||
"id": "9f53f04b7dd244658d59f515a14748b6"
|
"id": "local"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"logpush": false,
|
"logpush": false,
|
||||||
|
|||||||
@@ -4,12 +4,14 @@
|
|||||||
"main": "src/match.app.ts",
|
"main": "src/match.app.ts",
|
||||||
"compatibility_date": "2025-09-20",
|
"compatibility_date": "2025-09-20",
|
||||||
"compatibility_flags": ["nodejs_compat"],
|
"compatibility_flags": ["nodejs_compat"],
|
||||||
// Per-player presence store (room instance the player is currently in).
|
// Per-player presence store (room instance the player is currently in). Create
|
||||||
// Create with `wrangler kv namespace create RECFLARE_MATCH_PRESENCE` and set the id.
|
// with `wrangler kv namespace create RECFLARE_MATCH_PRESENCE`, then put the id in
|
||||||
|
// the root .env under RECFLARE_KV (see .env.example) — it is spliced into the
|
||||||
|
// "local" placeholder below at deploy time.
|
||||||
"kv_namespaces": [
|
"kv_namespaces": [
|
||||||
{
|
{
|
||||||
"binding": "RECFLARE_MATCH_PRESENCE",
|
"binding": "RECFLARE_MATCH_PRESENCE",
|
||||||
"id": "9f53f04b7dd244658d59f515a14748b6"
|
"id": "local"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
// Shared `recflare` DB (bound read-only here). The "local" placeholder is replaced
|
// Shared `recflare` DB (bound read-only here). The "local" placeholder is replaced
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ Player-settings worker served on the `playersettings` subdomain.
|
|||||||
## KV namespace
|
## KV namespace
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
wrangler kv namespace create RECFLARE_PLAYER_SETTINGS # then put the id in wrangler.jsonc
|
wrangler kv namespace create RECFLARE_PLAYER_SETTINGS # then put the id in .env under RECFLARE_KV (see .env.example)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
"kv_namespaces": [
|
"kv_namespaces": [
|
||||||
{
|
{
|
||||||
"binding": "RECFLARE_PLAYER_SETTINGS",
|
"binding": "RECFLARE_PLAYER_SETTINGS",
|
||||||
"id": "d33a90014e904b0eac720bddcbe0b036"
|
"id": "local"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"upload_source_maps": true,
|
"upload_source_maps": true,
|
||||||
|
|||||||
@@ -37,24 +37,69 @@ DIR=$(basename "$PWD")
|
|||||||
SUBDOMAIN=$(printf '%s' "$SUBDOMAINS_JSON" | jq -r --arg d "$DIR" '.[$d] // $d')
|
SUBDOMAIN=$(printf '%s' "$SUBDOMAINS_JSON" | jq -r --arg d "$DIR" '.[$d] // $d')
|
||||||
HOST="$SUBDOMAIN.$DOMAIN"
|
HOST="$SUBDOMAIN.$DOMAIN"
|
||||||
|
|
||||||
# Splice the shared `recflare` D1 database id from RECFLARE_D1 into a generated
|
# Splice deploy-time resource ids into a generated config. The committed
|
||||||
# config. All D1-backed workers bind the same database, so RECFLARE_D1 is a single
|
# wrangler.jsonc carries "local" placeholders so it needs no per-developer edits;
|
||||||
# id. The committed wrangler.jsonc carries a "local" placeholder so it needs no
|
# the real ids stay in the gitignored .env / CI secrets. Workers without these
|
||||||
# per-developer edits; the real id stays in the gitignored .env / CI secrets.
|
# bindings simply have no placeholder to replace.
|
||||||
# Workers without a D1 binding simply have no placeholder to replace.
|
#
|
||||||
|
# D1 — RECFLARE_D1: a single id (all workers share the one `recflare` database).
|
||||||
|
# KV — RECFLARE_KV: a JSON object keyed by binding name, since each KV namespace
|
||||||
|
# is distinct, e.g. {"RECFLARE_MATCH_PRESENCE":"…","RECFLARE_PLAYER_SETTINGS":"…"}.
|
||||||
CONFIG="wrangler.jsonc"
|
CONFIG="wrangler.jsonc"
|
||||||
if grep -q '"database_id": *"local"' wrangler.jsonc 2>/dev/null; then
|
NEEDS_D1=$(grep -q '"database_id": *"local"' wrangler.jsonc 2>/dev/null && echo 1 || true)
|
||||||
DB_ID=${RECFLARE_D1:-}
|
NEEDS_KV=$(grep -q '"id": *"local"' wrangler.jsonc 2>/dev/null && echo 1 || true)
|
||||||
if [ -z "$DB_ID" ]; then
|
|
||||||
echo "error: RECFLARE_D1 is not set — add the recflare D1 id to .env (see .env.example)" >&2
|
if [ -n "$NEEDS_D1" ] || [ -n "$NEEDS_KV" ]; then
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
CONFIG="wrangler.generated.jsonc"
|
CONFIG="wrangler.generated.jsonc"
|
||||||
# Generated alongside the original so its relative paths (main, migrations_dir)
|
# Generated alongside the original so its relative paths (main, migrations_dir)
|
||||||
# still resolve. Gitignored; removed on exit so `wrangler dev` is unaffected.
|
# still resolve. Gitignored; removed on exit so `wrangler dev` is unaffected.
|
||||||
trap 'rm -f wrangler.generated.jsonc' EXIT
|
trap 'rm -f wrangler.generated.jsonc wrangler.generated.tmp' EXIT
|
||||||
sed -E 's/("database_id"[[:space:]]*:[[:space:]]*")[^"]*(")/\1'"$DB_ID"'\2/' \
|
cp wrangler.jsonc wrangler.generated.jsonc
|
||||||
wrangler.jsonc >wrangler.generated.jsonc
|
|
||||||
|
if [ -n "$NEEDS_D1" ]; then
|
||||||
|
DB_ID=${RECFLARE_D1:-}
|
||||||
|
if [ -z "$DB_ID" ]; then
|
||||||
|
echo "error: RECFLARE_D1 is not set — add the recflare D1 id to .env (see .env.example)" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
sed -E 's/("database_id"[[:space:]]*:[[:space:]]*")[^"]*(")/\1'"$DB_ID"'\2/' \
|
||||||
|
wrangler.generated.jsonc >wrangler.generated.tmp
|
||||||
|
mv wrangler.generated.tmp wrangler.generated.jsonc
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$NEEDS_KV" ]; then
|
||||||
|
KV_JSON=${RECFLARE_KV:-}
|
||||||
|
[ -n "$KV_JSON" ] || KV_JSON='{}'
|
||||||
|
# Map binding name -> id, then replace each kv_namespaces `"id": "local"`
|
||||||
|
# with the id for its nearest preceding "binding" line. awk keeps state so
|
||||||
|
# multiple namespaces in one file each get the right id.
|
||||||
|
KV_PAIRS=$(printf '%s' "$KV_JSON" | jq -r 'to_entries[] | "\(.key)\t\(.value)"')
|
||||||
|
awk -v pairs="$KV_PAIRS" '
|
||||||
|
BEGIN {
|
||||||
|
n = split(pairs, lines, "\n")
|
||||||
|
for (i = 1; i <= n; i++) {
|
||||||
|
if (lines[i] == "") continue
|
||||||
|
t = index(lines[i], "\t")
|
||||||
|
id[substr(lines[i], 1, t - 1)] = substr(lines[i], t + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/"binding"[[:space:]]*:/ {
|
||||||
|
b = $0
|
||||||
|
sub(/.*"binding"[[:space:]]*:[[:space:]]*"/, "", b)
|
||||||
|
sub(/".*/, "", b)
|
||||||
|
curbind = b
|
||||||
|
}
|
||||||
|
/"id"[[:space:]]*:[[:space:]]*"local"/ {
|
||||||
|
if (!(curbind in id)) {
|
||||||
|
print "error: no KV id for binding [" curbind "] in RECFLARE_KV — add it to .env (see .env.example)" >"/dev/stderr"
|
||||||
|
exit 3
|
||||||
|
}
|
||||||
|
sub(/"local"/, "\"" id[curbind] "\"")
|
||||||
|
}
|
||||||
|
{ print }
|
||||||
|
' wrangler.generated.jsonc >wrangler.generated.tmp || exit 1
|
||||||
|
mv wrangler.generated.tmp wrangler.generated.jsonc
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Deploy with wrangler using the extracted values as binding variables
|
# Deploy with wrangler using the extracted values as binding variables
|
||||||
|
|||||||
Reference in New Issue
Block a user