#!/bin/sh # Docker entrypoint script for Claude Code Proxy # Starts both the Go proxy server and SvelteKit frontend set -e # Support user-supplied UID/GID via PUID/PGID env vars (default: 1000). PUID=${PUID:-1000} PGID=${PGID:-1000} # When running as root, fix data dir ownership and drop to the target user. if [ "$(id -u)" = "0" ]; then # Update the node user/group to match requested UID/GID if [ "$PUID" != "1000" ] || [ "$PGID" != "1000" ]; then deluser node 2>/dev/null || true delgroup node 2>/dev/null || true addgroup -g "$PGID" -S node adduser -S -u "$PUID" -G node -h /home/node -s /bin/sh node fi # Fix ownership of data dir and sqlite files (not postgres subdir) chown "$PUID:$PGID" /app/data 2>/dev/null || true chown "$PUID:$PGID" /app/data/requests.db* 2>/dev/null || true exec su-exec node "$0" "$@" fi echo "🚀 Starting Claude Code Proxy services..." echo "=========================================" # Function to handle graceful shutdown cleanup() { echo "" echo "🛑 Shutting down services..." kill $PROXY_PID $SVELTE_PID 2>/dev/null || true exit 0 } # Trap signals for graceful shutdown trap cleanup SIGTERM SIGINT echo "📊 Configuration:" echo " - Proxy Server: http://0.0.0.0:${PORT}" echo " - Svelte Dashboard: http://0.0.0.0:${SVELTE_PORT}" echo " - Database type: ${DB_TYPE:-sqlite}" if [ "${DB_TYPE}" = "postgres" ] || [ "${DB_TYPE}" = "postgresql" ]; then echo " - Database: PostgreSQL (via DATABASE_URL)" else echo " - Database: ${DB_PATH}" fi echo " - Anthropic API: ${ANTHROPIC_FORWARD_URL}" echo "=========================================" # Start proxy server echo "🔄 Starting proxy server..." PORT=${PORT} \ READ_TIMEOUT=${READ_TIMEOUT}s \ WRITE_TIMEOUT=${WRITE_TIMEOUT}s \ IDLE_TIMEOUT=${IDLE_TIMEOUT}s \ ANTHROPIC_FORWARD_URL=${ANTHROPIC_FORWARD_URL} \ ANTHROPIC_VERSION=${ANTHROPIC_VERSION} \ ANTHROPIC_MAX_RETRIES=${ANTHROPIC_MAX_RETRIES} \ DB_TYPE=${DB_TYPE:-sqlite} \ DB_PATH=${DB_PATH} \ DATABASE_URL=${DATABASE_URL:-} \ ./bin/proxy & PROXY_PID=$! # Wait for proxy to be ready echo "⏳ Waiting for proxy to be ready..." i=0 while [ $i -lt 30 ]; do if wget -qO /dev/null "http://localhost:${PORT}/health" 2>/dev/null; then echo "✅ Proxy is ready!" break fi sleep 1 i=$((i + 1)) done if [ $i -ge 30 ]; then echo "⚠️ Warning: proxy not ready after 30s, starting frontends anyway" fi # Start SvelteKit server echo "🔄 Starting SvelteKit server..." cd svelte PORT=${SVELTE_PORT} HOST=0.0.0.0 NODE_ENV=production DASHBOARD_PASSWORD="${DASHBOARD_PASSWORD}" node build/index.js & SVELTE_PID=$! cd .. echo "" echo "✨ All services started successfully!" echo "=========================================" echo "📊 Web Dashboard: http://localhost:${SVELTE_PORT}" echo "🔌 API Proxy: http://localhost:${PORT}" echo "💚 Health Check: http://localhost:${PORT}/health" echo "=========================================" echo "💡 To use with Claude Code, set: ANTHROPIC_BASE_URL=http://localhost:${PORT}" echo "" # Wait for processes to finish wait