#!/bin/bash # Configure CORS for MinIO buckets set -e CONTAINER_NAME="crew-minio-prod" MINIO_ALIAS="crewminio" DEPLOY_DIR="/var/www/apps/minio-storage" echo "==========================================" echo "Configuring CORS for MinIO buckets" echo "==========================================" echo "" # Load credentials from .env file echo "🔑 Loading MinIO credentials from .env" if [ -f "$DEPLOY_DIR/.env" ]; then set -a source "$DEPLOY_DIR/.env" set +a MINIO_USER="${MINIO_ROOT_USER}" MINIO_PASSWORD="${MINIO_ROOT_PASSWORD}" elif [ -f ".env" ]; then set -a source ".env" set +a MINIO_USER="${MINIO_ROOT_USER}" MINIO_PASSWORD="${MINIO_ROOT_PASSWORD}" else echo "⚠️ .env file not found, using defaults" MINIO_USER="minioadmin" MINIO_PASSWORD="minioadmin123" fi echo "✓ Credentials loaded" echo "" # Check if MinIO is running if ! docker ps | grep -q "$CONTAINER_NAME"; then echo "❌ Error: $CONTAINER_NAME container is not running" exit 1 fi # Configure MinIO client alias if not exists echo "📝 Configuring MinIO client alias..." docker exec $CONTAINER_NAME mc alias set $MINIO_ALIAS http://localhost:9000 $MINIO_USER $MINIO_PASSWORD # Buckets to configure BUCKETS=("crewsportswear" "merchbay" "merchbay-admin" "crew-admin" "email-reports") for bucket in "${BUCKETS[@]}"; do echo "Configuring CORS for bucket: $bucket" # Create CORS configuration file docker exec $CONTAINER_NAME sh -c "cat > /tmp/cors-${bucket}.json <<'EOF' { \"CORSRules\": [ { \"AllowedOrigins\": [ \"https://crewsportswear.app\", \"https://www.crewsportswear.app\", \"https://dev.crewsportswear.app\", \"https://merchbay.com\", \"https://www.merchbay.com\", \"https://dev.merchbay.app\", \"https://admin.merchbay.com\", \"https://crew-admin.app\", \"http://localhost:8080\", \"http://localhost:8081\", \"http://localhost:8082\" ], \"AllowedMethods\": [ \"GET\", \"HEAD\" ], \"AllowedHeaders\": [ \"*\" ], \"ExposeHeaders\": [ \"ETag\", \"Content-Type\", \"Content-Length\", \"Date\" ], \"MaxAgeSeconds\": 3600 } ] } EOF" # Apply CORS configuration to bucket docker exec $CONTAINER_NAME mc anonymous set-json $MINIO_ALIAS/$bucket < /tmp/cors-${bucket}.json 2>/dev/null || true # Alternative: Use MinIO's CORS API directly docker exec $CONTAINER_NAME sh -c "mc admin config set $MINIO_ALIAS api cors_allow_origin='https://crewsportswear.app,https://www.crewsportswear.app,https://dev.crewsportswear.app,https://merchbay.com,https://www.merchbay.com,https://dev.merchbay.app,https://admin.merchbay.com,https://crew-admin.app,http://localhost:8080,http://localhost:8081,http://localhost:8082'" 2>/dev/null || true echo " ✓ CORS configured for $bucket" done echo "" echo "🔄 Restarting MinIO to apply CORS settings..." docker exec $CONTAINER_NAME mc admin service restart $MINIO_ALIAS echo "" echo "==========================================" echo "✓ CORS configuration complete!" echo "==========================================" echo "" echo "Allowed origins:" echo " - https://crewsportswear.app" echo " - https://www.crewsportswear.app" echo " - https://dev.crewsportswear.app" echo " - https://merchbay.com" echo " - https://www.merchbay.com" echo " - https://dev.merchbay.app" echo " - https://admin.merchbay.com" echo " - https://crew-admin.app" echo " - http://localhost:8080-8082"