|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# The ai-gateway app serves a slice of the web app's routes, so it runs with the |
| 5 | +# web app's environment. PORT is left alone: the dev session exports it for the |
| 6 | +# web app. |
| 7 | +REPO_ROOT="$(git rev-parse --show-toplevel)" |
| 8 | +WEB_DIR="$REPO_ROOT/apps/web" |
| 9 | +OFFSET=${KILO_PORT_OFFSET:-0} |
| 10 | +AI_GATEWAY_PORT=${AI_GATEWAY_PORT:-$((3010 + OFFSET))} |
| 11 | + |
| 12 | +# Next.js loads .env* files from CWD. Link the files the web app reads: its |
| 13 | +# committed .env, plus the local files from apps/web or, failing that, the repo |
| 14 | +# root (the same fallback scripts/dev.sh applies for the web app). |
| 15 | +link_env_file() { |
| 16 | + local name="$1" |
| 17 | + local source="" |
| 18 | + if [ -e "$WEB_DIR/$name" ]; then |
| 19 | + source="$WEB_DIR/$name" |
| 20 | + elif [ -e "$REPO_ROOT/$name" ]; then |
| 21 | + source="$REPO_ROOT/$name" |
| 22 | + fi |
| 23 | + if [ -n "$source" ] && [ ! -e "$name" ]; then |
| 24 | + ln -s "$source" "$name" |
| 25 | + echo "Symlinked $source → $(pwd)/$name" |
| 26 | + fi |
| 27 | +} |
| 28 | +for envfile in .env .env.local .env.development.local; do |
| 29 | + link_env_file "$envfile" |
| 30 | +done |
| 31 | + |
| 32 | +# Read a value from the linked env files without sourcing them. Exported |
| 33 | +# process env beats Next's own .env loading, so each default below only applies |
| 34 | +# when neither the shell nor the env files set the variable, as in the web app. |
| 35 | +read_env_value() { |
| 36 | + local key="$1" |
| 37 | + local file |
| 38 | + local value |
| 39 | + |
| 40 | + for file in .env.development.local .env.local; do |
| 41 | + if [ -f "$file" ]; then |
| 42 | + value=$(awk -F= -v key="$key" ' |
| 43 | + $1 == key { |
| 44 | + value = substr($0, length(key) + 2) |
| 45 | + gsub(/^["'\'']|["'\'']$/, "", value) |
| 46 | + print value |
| 47 | + exit |
| 48 | + } |
| 49 | + ' "$file") |
| 50 | + if [ -n "$value" ]; then |
| 51 | + echo "$value" |
| 52 | + return |
| 53 | + fi |
| 54 | + fi |
| 55 | + done |
| 56 | +} |
| 57 | + |
| 58 | +REDIS_URL="${REDIS_URL:-$(read_env_value REDIS_URL)}" |
| 59 | +export REDIS_URL="${REDIS_URL:-redis://localhost:6379}" |
| 60 | +UPSTASH_REDIS_REST_URL="${UPSTASH_REDIS_REST_URL:-$(read_env_value UPSTASH_REDIS_REST_URL)}" |
| 61 | +export UPSTASH_REDIS_REST_URL="${UPSTASH_REDIS_REST_URL:-http://localhost:8079}" |
| 62 | +export UPSTASH_REDIS_REST_TOKEN="${UPSTASH_REDIS_REST_TOKEN:-example_token}" |
| 63 | +# Links in responses, such as sign-in and billing URLs, must point at the web |
| 64 | +# app rather than at this app. |
| 65 | +WEB_PORT=$(cat "$REPO_ROOT/.dev-port" 2>/dev/null || echo $((3000 + OFFSET))) |
| 66 | +APP_URL_OVERRIDE="${APP_URL_OVERRIDE:-$(read_env_value APP_URL_OVERRIDE)}" |
| 67 | +export APP_URL_OVERRIDE="${APP_URL_OVERRIDE:-http://localhost:$WEB_PORT}" |
| 68 | +NEXTAUTH_URL="${NEXTAUTH_URL:-$(read_env_value NEXTAUTH_URL)}" |
| 69 | +export NEXTAUTH_URL="${NEXTAUTH_URL:-$APP_URL_OVERRIDE}" |
| 70 | + |
| 71 | +echo "ai-gateway dev server starting on port $AI_GATEWAY_PORT" |
| 72 | +exec next dev -H "${NEXT_DEV_HOSTNAME:-0.0.0.0}" -p "$AI_GATEWAY_PORT" "$@" |
0 commit comments