| 1 | #!/usr/bin/env bash |
| 2 | # π€ tools/marketing-shots/serve.sh β build (if needed) + run the local demo |
| 3 | # instance, seed the "The Commons" civic-tech space, print the URLs + a |
| 4 | # localhost sign-in link. Wrapped by `./tools/dev shots up`. |
| 5 | # |
| 6 | # Foreground: it starts the server, seeds it, prints everything, then blocks so |
| 7 | # the space stays live for capturing. Ctrl-C stops it. The DB + state are wiped |
| 8 | # on each start (a clean, reproducible space every run) unless --keep. |
| 9 | # |
| 10 | # ./tools/marketing-shots/serve.sh # fresh seed on port 8848 |
| 11 | # ./tools/marketing-shots/serve.sh --keep # reuse the existing DB (don't re-seed) |
| 12 | # LCHAT_DEMO_PORT=9000 ./tools/marketing-shots/serve.sh |
| 13 | # |
| 14 | # Data + isolation: DB lives at tools/marketing-shots/.demo.db (gitignored, |
| 15 | # wiped each run). Admin token is a throwaway fixed value. Nothing here touches |
| 16 | # any real/production instance. |
| 17 | set -euo pipefail |
| 18 | |
| 19 | ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 20 | cd "$ROOT" |
| 21 | KIT="$ROOT/tools/marketing-shots" |
| 22 | |
| 23 | PORT="${LCHAT_DEMO_PORT:-8848}" |
| 24 | DB="$KIT/.demo.db" |
| 25 | ADMIN="${LCHAT_DEMO_ADMIN:-capkit-demo-admin}" |
| 26 | GROUP_NAME="The Commons" |
| 27 | KEEP=0 |
| 28 | [ "${1:-}" = "--keep" ] && KEEP=1 |
| 29 | |
| 30 | # Build the binary from THIS worktree if it's missing (or --rebuild). |
| 31 | if [ ! -x "$ROOT/lchat" ] || [ "${1:-}" = "--rebuild" ]; then |
| 32 | echo "serve: building lchat from this worktreeβ¦" |
| 33 | GOTOOLCHAIN=auto go build -o "$ROOT/lchat" . |
| 34 | fi |
| 35 | |
| 36 | # Refuse to trample another process on the port. |
| 37 | if lsof -ti "tcp:$PORT" >/dev/null 2>&1; then |
| 38 | echo "serve: something is already listening on port $PORT β stop it or set LCHAT_DEMO_PORT." >&2 |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
| 42 | if [ "$KEEP" = "0" ]; then |
| 43 | rm -f "$DB" "$DB-wal" "$DB-shm" "$KIT/.demo-state.json" |
| 44 | fi |
| 45 | |
| 46 | echo "serve: starting server on 127.0.0.1:$PORT (db $(basename "$DB"))β¦" |
| 47 | LCHAT_DB="$DB" LCHAT_ADMIN_TOKEN="$ADMIN" LCHAT_GROUP_NAME="$GROUP_NAME" LCHAT_SKIP_SETUP=1 \ |
| 48 | ADDR="127.0.0.1:$PORT" "$ROOT/lchat" & |
| 49 | SERVER_PID=$! |
| 50 | trap 'echo; echo "serve: stopping serverβ¦"; kill "$SERVER_PID" 2>/dev/null || true' EXIT INT TERM |
| 51 | |
| 52 | # Wait for it to answer. |
| 53 | for _ in $(seq 1 100); do |
| 54 | curl -sf "http://127.0.0.1:$PORT/api/version" >/dev/null 2>&1 && break |
| 55 | sleep 0.1 |
| 56 | done |
| 57 | curl -sf "http://127.0.0.1:$PORT/api/version" >/dev/null 2>&1 || { echo "serve: server didn't come up" >&2; exit 1; } |
| 58 | |
| 59 | LINK="" |
| 60 | if [ "$KEEP" = "0" ] || [ ! -f "$KIT/.demo-state.json" ]; then |
| 61 | echo "serve: seeding The Commonsβ¦" |
| 62 | LINK="$(node tools/marketing-shots/seed-team.mjs --base "http://127.0.0.1:$PORT" --admin "$ADMIN")" |
| 63 | else |
| 64 | echo "serve: --keep: reusing existing space; minting a fresh sign-in linkβ¦" |
| 65 | LINK="$(node tools/marketing-shots/device-link.mjs "http://127.0.0.1:$PORT")" |
| 66 | fi |
| 67 | |
| 68 | cat <<EOF |
| 69 | |
| 70 | ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 71 | β Demo space "The Commons" is live. |
| 72 | |
| 73 | Local URL: http://127.0.0.1:$PORT/ |
| 74 | Admin: http://127.0.0.1:$PORT/admin#token=$ADMIN |
| 75 | Web check: ./tools/dev shots web-check shot.png (headless 2x screenshot) |
| 76 | |
| 77 | Sign-in link (localhost β one-time, signs you in as "Cass"): |
| 78 | $LINK |
| 79 | |
| 80 | For the NATIVE apps use the tunnel instead (real https): |
| 81 | In another terminal: ./tools/dev shots tunnel |
| 82 | It prints https://try-*.11relay.com links to open in the Mac app + on the iPhone. |
| 83 | |
| 84 | Ctrl-C stops the server. |
| 85 | ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 86 | |
| 87 | EOF |
| 88 | |
| 89 | wait "$SERVER_PID" |
| 90 | |