#!/usr/bin/env bash set -euo pipefail # Sets up NGINX as a reverse proxy + load balancer across Pi nodes # Run this on a Pi that will act as the entry point (or any Pi with NGINX) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" if [ -f "$PROJECT_ROOT/.env" ]; then set -a source "$PROJECT_ROOT/.env" set +a fi PORT="${PORT:-3000}" PI_HOSTS=("${PI_HOST_1:-}" "${PI_HOST_2:-}" "${PI_HOST_3:-}") # Filter out empty hosts ACTIVE_HOSTS=() for host in "${PI_HOSTS[@]}"; do if [ -n "$host" ]; then ACTIVE_HOSTS+=("$host") fi done echo "Setting up NGINX load balancer for ${#ACTIVE_HOSTS[@]} Pi nodes..." # Install nginx if needed if ! command -v nginx &> /dev/null; then sudo apt-get update && sudo apt-get install -y nginx fi # Build upstream block UPSTREAM="" for host in "${ACTIVE_HOSTS[@]}"; do UPSTREAM+=" server ${host}:${PORT};\n" done # Write nginx config sudo tee /etc/nginx/sites-available/blackroad-stripe > /dev/null </api/webhooks/stripe"