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