Skip to content
95 linesCodeBlameRaw
1#!/usr/bin/env bash
2# Starts self-hosted g1t inside its container: makes the keys it needs once,
3# writes the Wrangler configs, brings every database up to date, and runs
4# every Worker in one workerd on :8787.
5set -euo pipefail
6
7cd "$(dirname "$0")"
8HERE="$(pwd)"
9DATA="${G1T_DATA:-/data}"
10STATE="$DATA/state"
11KEYS="$DATA/keys.env"
12GENERATED="$DATA/generated"
13WRANGLER="$(cd ../.. && pwd)/node_modules/.bin/wrangler"
14mkdir -p "$STATE"
15
16# Keys that seal secrets at rest (actions, integrations, webhooks). Made on
17# first start and kept on the volume: losing them loses those secrets.
18if [ ! -f "$KEYS" ]; then
19 umask 077
20 {
21 echo "ACTIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')"
22 echo "INTEGRATIONS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')"
23 echo "WEBHOOKS_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')"
24 } > "$KEYS"
25fi
26# Identity's key seals the GitHub tokens of linked accounts; added to keys
27# made before it existed.
28if ! grep -q '^IDENTITY_KEY=' "$KEYS"; then
29 echo "IDENTITY_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" >> "$KEYS"
30fi
31# The site's key, which signs the short-lived addresses of private
32# repositories' files.
33if ! grep -q '^USERCONTENT_KEY=' "$KEYS"; then
34 echo "USERCONTENT_KEY=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" >> "$KEYS"
35fi
36# The packages service's key, which signs registry tokens.
37if ! grep -q '^PACKAGES_TOKEN_SECRET=' "$KEYS"; then
38 echo "PACKAGES_TOKEN_SECRET=$(node -e 'console.log(require("crypto").randomBytes(32).toString("hex"))')" >> "$KEYS"
39fi
40set -a
41# shellcheck disable=SC1090
42. "$KEYS"
43set +a
44
45# The git store's shared secret: given, or the file the git store made.
46if [ -z "${GITSTORE_SECRET:-}" ] && [ -n "${GITSTORE_SECRET_FILE:-}" ]; then
47 for _ in $(seq 1 60); do [ -s "$GITSTORE_SECRET_FILE" ] && break; sleep 1; done
48 GITSTORE_SECRET="$(cat "$GITSTORE_SECRET_FILE")"
49 export GITSTORE_SECRET
50fi
51[ -n "${GITSTORE_SECRET:-}" ] || { echo "No GITSTORE_SECRET or GITSTORE_SECRET_FILE." >&2; exit 1; }
52
53node configs.mjs "$GENERATED"
54cd "$GENERATED"
55
56# Migrations: the same files D1 gets, applied to the SQLite files on the
57# volume. Applied ones are recorded, so this is safe on every start.
58for config in $(cat workers.txt); do
59 for database in $(node -e "for (const d of require('./$config').d1_databases ?? []) console.log(d.database_name)"); do
60 echo "Migrating $database"
61 "$WRANGLER" d1 migrations apply "$database" --local --persist-to "$STATE" -c "$config" >/dev/null
62 done
63done
64
65args=()
66while read -r config; do args+=(-c "$config"); done < workers.txt
67
68# workerd fires no cron triggers: the scheduler runs the services' crons
69# (schedules.json, from configs.mjs) once a minute through Wrangler's local
70# API, which answers only here, on localhost.
71node "$HERE/scheduler.mjs" schedules.json http://127.0.0.1:8787 &
72
73# The API (REST, MCP, OAuth) on a port of its own, in a second workerd. Its
74# service bindings find the Workers below through Wrangler's dev registry,
75# so it waits until they are up. It holds no data; its own KV (Actions
76# artifacts, which need the runner) is kept apart from the site's. It
77# listens on 8789 in the container; API_PORT is the port people use, which
78# API_URL is derived from (configs.mjs).
79(
80 for _ in $(seq 1 120); do
81 node -e "fetch('http://127.0.0.1:8787/').then(() => process.exit(0), () => process.exit(1))" && break
82 sleep 2
83 done
84 echo "The API is starting on $(node -p "require('./api.json').api") (MCP: $(node -p "require('./api.json').mcp"))"
85 exec "$WRANGLER" dev -c g1t-api.json \
86 --ip 0.0.0.0 --port 8789 \
87 --persist-to "$DATA/api-state" \
88 --show-interactive-dev-session=false
89) &
90
91echo "g1t is starting on ${PUBLIC_URL:-http://localhost:8787}"
92exec "$WRANGLER" dev "${args[@]}" \
93 --ip 0.0.0.0 --port 8787 \
94 --persist-to "$STATE" \
95 --show-interactive-dev-session=false