#!/usr/bin/env bash # One command to see the app in a browser: build it with -env # development (cmd/docs/main.go's -env flag — see docs.Config.Env's # own doc comment for why this is a flag of its own rather than # riding on some other one), against a scratch database that starts # empty every run, on a fixed local port so the URL below is always # the same one to open. # # Never for anything but a laptop: -env development is what lets an # empty $DOCS_INSTANCE_KEY and unconfigured mail default instead of # refusing to start (internal/docs/auth.go), and what makes a magic # link land in this process's own log instead of a real mailbox — # fine for a throwaway local database, never for anything real. There # used to also be a development-only stand-in identity gated the same # way (internal/docs/devauth.go, turned on with -dev-auth); it had # zero readers once real sign-in landed and was deleted in fix round # 1 on Task 1 — this script now signs in for real, the same magic # link a real deployment would use, just logged instead of mailed. set -euo pipefail cd "$(dirname "$0")/.." port="${DOCS_DEV_PORT:-8099}" addr="localhost:${port}" # -seed (cmd/docs/main.go) populates the fresh database above with # CARLOS Docs' own sample documents (internal/docs/seed.go) — real # English prose applied through the CRDT, not lorem ipsum — so the # canvas, the typography and the list have something worth looking at # from the first run rather than two words in an empty document. # Gated to Env == development the same way this whole script is (see # the header comment above): -seed refuses outright against anything # else, so it can never reach a database that might hold real work. # $DOCS_SEED_ADDRESS overrides which address gets the sample set, # same DOCS_DEV_PORT-style override as above; unset, main.go's own # devSeedAddress ("dev@example.com") is used. seed_address="${DOCS_SEED_ADDRESS:-dev@example.com}" # A scratch database per run, not docs.db in the repo root: this # script exists so anyone can see the app from nothing, not so it # accumulates state nobody wrote down. mktemp -d rather than a fixed # path under /tmp, so two concurrent runs (two people, or a person and # a CI job) never collide or clobber each other's database. # # The EXIT trap below only ever fires while this is still a running # bash script — up through a failed build. It is deliberately gone the # moment exec below replaces this process with the server (see that # line's own comment): fix round 1 found that running the server as a # plain foreground subprocess instead left the wrapper's PID and the # server's PID different, so killing `./scripts/run-dev.sh &` — the # exact pattern this script invites — fired this trap and deleted the # scratch directory while orphaning a server still holding the port, # undeletable and needing `kill -9` by hand. exec fixes the orphan at # the cost of this cleanup after the server itself is running; a # scratch directory under $TMPDIR left behind by a killed dev server is # a smaller problem than a leaked listener nothing can reach again. scratch="$(mktemp -d "${TMPDIR:-/tmp}/docs-dev.XXXXXX")" db="${scratch}/docs.db" trap 'rm -rf "$scratch"' EXIT export CGO_ENABLED=0 go build -o "${scratch}/docs" ./cmd/docs # No $DOCS_ORIGIN needed here: -addr is a TCP address, so # cmd/docs/main.go derives the origin from it directly (the same host # a browser hitting this address sends back as its own Origin header). # That derivation only ever fires on this local/dev path — the real # platform activation is a unix socket with no -addr to derive # anything from, which is exactly why $DOCS_ORIGIN must be set # explicitly there (internal/docs/app.go's Config.Origin doc comment). echo "docs: dev database at ${db}" echo "docs: http://${addr}/" echo "docs: seeded sample documents for ${seed_address} — sign in with that address to see them" # exec, not a plain invocation: this script's own PID becomes the # server's PID, so a signal sent to it (Ctrl-C, or `kill` on the # backgrounded `./scripts/run-dev.sh &`) reaches the server directly — # rastrillo.Serve's own SIGTERM/SIGINT drain runs instead of orphaning # a listener nothing can reach or clean up afterward. See the comment # above the trap for what this costs. exec "${scratch}/docs" -env development -seed -addr "${addr}" -db "${db}"