laion-tunes / start-frontend.sh
Christoph Schuhmann
Update web UI, server, and add deployment scripts
bf91668
#!/usr/bin/env bash
# ============================================================================
# LAION-Tunes Frontend Startup Script
# ============================================================================
# Serves the LAION-Tunes web UI via HTTPS using Caddy as a reverse proxy.
# Caddy serves index.html for the root and proxies /api/* requests to the
# FastAPI backend, then cloudflared provides a public HTTPS tunnel.
#
# Usage:
# ./start-frontend.sh # defaults
# ./start-frontend.sh --port 8080 # custom frontend port
# ./start-frontend.sh --backend http://host:7860 # custom backend URL
#
# Prerequisites:
# - caddy (available at /root/.local/bin/caddy)
# - cloudflared binary at ../cloudflared
# - Backend must be running (start-backend.sh)
# ============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# ── Configuration ────────────────────────────────────────────────────────────
FRONTEND_PORT="${FRONTEND_PORT:-8080}"
BACKEND_URL="${BACKEND_URL:-http://localhost:7860}"
CADDY="$(command -v caddy 2>/dev/null || echo /root/.local/bin/caddy)"
CLOUDFLARED="$SCRIPT_DIR/../cloudflared"
LOG_DIR="$SCRIPT_DIR/logs"
# Parse args
while [[ $# -gt 0 ]]; do
case "$1" in
--port) FRONTEND_PORT="$2"; shift 2 ;;
--backend) BACKEND_URL="$2"; shift 2 ;;
*) echo "Unknown arg: $1"; exit 1 ;;
esac
done
mkdir -p "$LOG_DIR"
echo "============================================================"
echo " LAION-Tunes Frontend Startup"
echo "============================================================"
echo " Frontend port: $FRONTEND_PORT"
echo " Backend URL: $BACKEND_URL"
echo " Caddy: $CADDY"
echo "============================================================"
# ── Verify prerequisites ─────────────────────────────────────────────────────
if [ ! -x "$CADDY" ]; then
echo "ERROR: caddy not found at $CADDY"
echo "Install: curl -fsSL https://caddyserver.com/api/download?os=linux&arch=amd64 -o /usr/local/bin/caddy && chmod +x /usr/local/bin/caddy"
exit 1
fi
if [ ! -x "$CLOUDFLARED" ]; then
echo "ERROR: cloudflared not found at $CLOUDFLARED"
exit 1
fi
if [ ! -f "$SCRIPT_DIR/index.html" ]; then
echo "ERROR: index.html not found in $SCRIPT_DIR"
exit 1
fi
# ── Step 1: Check backend is reachable ───────────────────────────────────────
echo ""
echo "[1/3] Checking backend connectivity..."
if curl -sf "${BACKEND_URL}/api/stats" >/dev/null 2>&1; then
echo " Backend is reachable at $BACKEND_URL"
else
echo " WARNING: Backend not reachable at $BACKEND_URL"
echo " Make sure start-backend.sh is running first."
echo " Continuing anyway (Caddy will retry automatically)..."
fi
# ── Step 2: Start Caddy reverse proxy ────────────────────────────────────────
echo ""
echo "[2/3] Starting Caddy on port $FRONTEND_PORT..."
# Kill existing caddy on this port
fuser -k "${FRONTEND_PORT}/tcp" 2>/dev/null || true
sleep 1
# Write Caddyfile
cat > "$SCRIPT_DIR/Caddyfile" <<CADDYEOF
{
admin off
auto_https off
}
:${FRONTEND_PORT} {
# Serve static files (index.html, nsfw report, etc.)
root * ${SCRIPT_DIR}
# Proxy all API requests to the FastAPI backend
handle /api/* {
reverse_proxy ${BACKEND_URL}
}
# Proxy the NSFW report page
handle /nsfw-report {
reverse_proxy ${BACKEND_URL}
}
# Serve index.html for root
handle {
try_files {path} /index.html
file_server
}
# Enable gzip
encode gzip
# CORS headers (backend already handles this, but belt-and-suspenders)
header {
Access-Control-Allow-Origin *
Access-Control-Allow-Methods "GET, POST, OPTIONS"
Access-Control-Allow-Headers "Content-Type"
}
log {
output file ${LOG_DIR}/caddy-access.log
}
}
CADDYEOF
nohup "$CADDY" run --config "$SCRIPT_DIR/Caddyfile" \
> "$LOG_DIR/caddy.log" 2>&1 &
CADDY_PID=$!
echo " Caddy PID: $CADDY_PID"
echo "$CADDY_PID" > "$LOG_DIR/caddy-frontend.pid"
# Wait for Caddy to start
sleep 2
if curl -sf "http://localhost:${FRONTEND_PORT}/" >/dev/null 2>&1; then
echo " Caddy ready."
else
echo " WARNING: Caddy may not be ready yet. Check: tail -f $LOG_DIR/caddy.log"
fi
# ── Step 3: Start cloudflared tunnel for public HTTPS ────────────────────────
echo ""
echo "[3/3] Starting cloudflared tunnel for public HTTPS..."
# Kill existing tunnel if running
if [ -f "$LOG_DIR/cloudflared-frontend.pid" ]; then
kill "$(cat "$LOG_DIR/cloudflared-frontend.pid")" 2>/dev/null || true
fi
nohup "$CLOUDFLARED" tunnel --url "http://localhost:${FRONTEND_PORT}" \
> "$LOG_DIR/cloudflared-frontend.log" 2>&1 &
CF_PID=$!
echo "$CF_PID" > "$LOG_DIR/cloudflared-frontend.pid"
# Wait for tunnel URL
sleep 5
TUNNEL_URL=""
for i in $(seq 1 30); do
TUNNEL_URL=$(grep -oP 'https://[a-z0-9-]+\.trycloudflare\.com' "$LOG_DIR/cloudflared-frontend.log" 2>/dev/null | head -1)
if [ -n "$TUNNEL_URL" ]; then
break
fi
sleep 1
done
echo ""
echo "============================================================"
echo " LAION-Tunes Frontend is running!"
echo "============================================================"
echo ""
echo " Local: http://localhost:${FRONTEND_PORT}"
if [ -n "$TUNNEL_URL" ]; then
echo " Public: $TUNNEL_URL"
else
echo " Public: (tunnel URL pending, check: grep trycloudflare $LOG_DIR/cloudflared-frontend.log)"
fi
echo ""
echo " Logs:"
echo " Caddy: tail -f $LOG_DIR/caddy.log"
echo " Access: tail -f $LOG_DIR/caddy-access.log"
echo " Cloudflared: tail -f $LOG_DIR/cloudflared-frontend.log"
echo ""
echo " Stop everything:"
echo " kill $CADDY_PID $CF_PID"
echo "============================================================"