From 49551b3f0feab834388a6b438741e1359e0e9bf0 Mon Sep 17 00:00:00 2001 From: SirTenzin <49116958+SirTenzin@users.noreply.github.com> Date: Fri, 17 Apr 2026 17:31:08 +0000 Subject: [PATCH] feat: Google OAuth emulator + port args for bun dev:agent" --- package.json | 2 +- scripts/dev.ts | 12 ++++++-- scripts/setup/AGENT_README.md | 29 +++++++++++++++++--- scripts/setup/agent-services.sh | 41 ++++++++++++++++++++++++++-- scripts/setup/devAgent.sh | 13 +++++++++ scripts/setup/google-oauth-seed.yaml | 9 ++++++ scripts/setup/writeAgentEnv.ts | 11 ++++++-- 7 files changed, 103 insertions(+), 14 deletions(-) create mode 100755 scripts/setup/devAgent.sh create mode 100644 scripts/setup/google-oauth-seed.yaml diff --git a/package.json b/package.json index 2bb143b99..2ebb051aa 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "setup:s3-admin": "bun scripts/setup/setupS3Admin.ts", "setup:test": "infisical run --env=dev -- bun scripts/setup/setup-test.ts", "agent:bootstrap": "bash scripts/setup/agent-bootstrap.sh", - "dev:agent": "bash scripts/setup/agent-services.sh && bun scripts/dev.ts", + "dev:agent": "bash scripts/setup/devAgent.sh", "migrate-functions": "infisical run --env=dev -- bun scripts/migrations/migrate-functions.ts", "migrate-functions:test": "infisical run --env=test -- bun scripts/migrations/migrate-functions.ts", "migrate-functions:prod": "infisical run --env=prod -- bun scripts/migrations/migrate-functions.ts", diff --git a/scripts/dev.ts b/scripts/dev.ts index a13d1e143..0219e1126 100644 --- a/scripts/dev.ts +++ b/scripts/dev.ts @@ -9,9 +9,15 @@ const worktreeNum = : 1; const portOffset = (worktreeNum - 1) * 100; -const VITE_PORT = 3000 + portOffset; -const SERVER_PORT = 8080 + portOffset; -const CHECKOUT_PORT = 3001 + portOffset; +const VITE_PORT = process.env.VITE_PORT + ? Number.parseInt(process.env.VITE_PORT, 10) + : 3000 + portOffset; +const SERVER_PORT = process.env.SERVER_PORT + ? Number.parseInt(process.env.SERVER_PORT, 10) + : 8080 + portOffset; +const CHECKOUT_PORT = process.env.CHECKOUT_PORT + ? Number.parseInt(process.env.CHECKOUT_PORT, 10) + : 3001 + portOffset; const skipWorkers = worktreeNum > 1; const isProductionMode = process.argv.includes("--production"); diff --git a/scripts/setup/AGENT_README.md b/scripts/setup/AGENT_README.md index be9148216..880bf427c 100644 --- a/scripts/setup/AGENT_README.md +++ b/scripts/setup/AGENT_README.md @@ -19,10 +19,11 @@ Starts all local services, creates the database, writes env files, runs migratio 1. Starts `postgresql`, `redis-server`, and `clickhouse-server` via `service` 2. Starts ElasticMQ in the background on `:9324` (skipped if already running) -3. Creates the `autumn` Postgres database if it does not exist -4. Writes `server/.env` (skips if already present) and `vite/.env` from `vite/.env.example` -5. Runs `bun db:migrate` -6. Launches server `:8080`, vite `:3000`, checkout `:3001`, and workers via `scripts/dev.ts` +3. Starts the Google OAuth emulator in the background on `:4002` (skipped if already running) +4. Creates the `autumn` Postgres database if it does not exist +5. Writes `server/.env` (skips if already present) and `vite/.env` from `vite/.env.example` +6. Runs `bun db:migrate` +7. Launches server `:8080`, vite `:3000`, checkout `:3001`, and workers via `scripts/dev.ts` ## Static team-wide env vars @@ -51,6 +52,7 @@ Set these in the process environment before running `bun dev:agent` and they wil | Redis | 6379 | Used for `CACHE_URL` and `CACHE_URL_US_EAST` | | ElasticMQ | 9324 | Local SQS replacement, queue: `autumn.fifo` | | ClickHouse | 8123 | Used for `TINYBIRD_CLICKHOUSE_URL` | +| Google OAuth emulator | 4002 | Local emulator for Google sign-in | | Server | 8080 | Autumn API server | | Vite | 3000 | Frontend dev server | | Checkout | 3001 | Checkout app dev server | @@ -72,3 +74,22 @@ To regenerate `server/.env` with fresh secrets: ```bash rm server/.env vite/.env && bun dev:agent ``` + +## Custom ports + +```bash +bun dev:agent 8181 3100 # server on :8181, frontend on :3100 +``` + +If you change ports after `server/.env` has already been generated, delete it first: + +```bash +rm server/.env vite/.env && bun dev:agent 8181 3100 +``` + +## Google OAuth (local emulator) + +The Google OAuth emulator starts automatically at `:4002` during `bun dev:agent`. Sign in with Google works out of the box using the test user: + +- Email: `testuser@example.com` +- Password: any (the emulator accepts all logins) diff --git a/scripts/setup/agent-services.sh b/scripts/setup/agent-services.sh index 555502672..d0b71035e 100755 --- a/scripts/setup/agent-services.sh +++ b/scripts/setup/agent-services.sh @@ -33,16 +33,51 @@ if ! pgrep -f 'elasticmq.*\.jar' >/dev/null 2>&1; then fi fi -# --- 3. Ensure Postgres DB exists --- +# --- 3. Google OAuth emulator --- +if ! pgrep -f 'emulate.*google' >/dev/null 2>&1; then + log "Starting Google OAuth emulator on :4002" + sudo mkdir -p /var/log/autumn && sudo chmod 0777 /var/log/autumn + lsof -ti:4002 | xargs kill -9 2>/dev/null || true + _GOOGLE_SEED_PORT="${AGENT_SERVER_PORT:-8080}" + cat > /tmp/google-oauth-seed.yaml </var/log/autumn/google-oauth-emulator.log 2>&1 & + disown || true + log "Waiting for Google OAuth emulator to be ready" + EMULATOR_READY=0 + for i in $(seq 1 30); do + if curl -sf -o /dev/null http://localhost:4002; then + EMULATOR_READY=1 + break + fi + sleep 0.5 + done + if [ "$EMULATOR_READY" -eq 0 ]; then + echo "[agent-services] ERROR: Google OAuth emulator did not become ready after 15s. Check /var/log/autumn/google-oauth-emulator.log" >&2 + exit 1 + fi +fi + +# --- 4. Ensure Postgres DB exists --- DB_NAME="autumn" sudo -u postgres psql -tc "SELECT 1 FROM pg_database WHERE datname = '$DB_NAME'" 2>/dev/null \ | grep -q 1 || sudo -u postgres createdb "$DB_NAME" sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres';" >/dev/null 2>&1 || true -# --- 4. Write env files (no-op if server/.env already exists) --- +# --- 5. Write env files (no-op if server/.env already exists) --- bun scripts/setup/writeAgentEnv.ts -# --- 5. DB migrations --- +# --- 6. DB migrations --- log "Running migrations" bun db:generate >/dev/null 2>&1 || true bun db:migrate diff --git a/scripts/setup/devAgent.sh b/scripts/setup/devAgent.sh new file mode 100755 index 000000000..a5be075fa --- /dev/null +++ b/scripts/setup/devAgent.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -euo pipefail + +SERVER_PORT="${1:-8080}" +VITE_PORT="${2:-3000}" + +export AGENT_SERVER_PORT="$SERVER_PORT" +export AGENT_VITE_PORT="$VITE_PORT" + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +bash "$DIR/agent-services.sh" + +exec SERVER_PORT="$SERVER_PORT" VITE_PORT="$VITE_PORT" bun scripts/dev.ts diff --git a/scripts/setup/google-oauth-seed.yaml b/scripts/setup/google-oauth-seed.yaml new file mode 100644 index 000000000..8e70cf832 --- /dev/null +++ b/scripts/setup/google-oauth-seed.yaml @@ -0,0 +1,9 @@ +google: + users: + - email: testuser@example.com + name: Test User + oauth_clients: + - client_id: my-client-id.apps.googleusercontent.com + client_secret: GOCSPX-secret + redirect_uris: + - http://localhost:8080/api/auth/callback/google diff --git a/scripts/setup/writeAgentEnv.ts b/scripts/setup/writeAgentEnv.ts index 804ec9be9..512635f18 100644 --- a/scripts/setup/writeAgentEnv.ts +++ b/scripts/setup/writeAgentEnv.ts @@ -14,6 +14,9 @@ const genUrlSafeBase64 = ({ bytes }: { bytes: number }): string => .replace(/\//g, "_") .replace(/=+$/g, ""); +const serverPort = process.env.AGENT_SERVER_PORT ?? "8080"; +const vitePort = process.env.AGENT_VITE_PORT ?? "3000"; + if (existsSync(serverEnvPath)) { console.log("[writeAgentEnv] server/.env already exists — skipping generation"); } else { @@ -62,11 +65,13 @@ AWS_SECRET_ACCESS_KEY=x TINYBIRD_CLICKHOUSE_URL=http://localhost:8123 # App URLs -BETTER_AUTH_URL=http://localhost:8080 -CLIENT_URL=http://localhost:3000 -STRIPE_WEBHOOK_URL=http://localhost:8080 +BETTER_AUTH_URL=http://localhost:${serverPort} +CLIENT_URL=http://localhost:${vitePort} +STRIPE_WEBHOOK_URL=http://localhost:${serverPort} # Static team-wide (pass-through from process.env) +GOOGLE_CLIENT_ID=my-client-id.apps.googleusercontent.com +GOOGLE_CLIENT_SECRET=GOCSPX-secret ${passLines} # Environment