#!/bin/bash
# Phoenix Development Startup Script
# This script sets up port forwarding to Traefik and SMTP and starts Phoenix
echo "π Starting Phoenix Development Environment"
echo "==========================================="
# Function to wait for Traefik to be ready
wait_for_traefik() {
echo "β³ Waiting for Traefik at traefik:80..."
for i in {1..30}; do
if nc -z traefik 80 2>/dev/null; then
echo "β
Traefik is ready"
return 0
fi
echo " Attempt $i/30: Traefik not ready, waiting..."
sleep 2
done
echo "β Traefik not available after 60 seconds"
return 1
}
# Function to wait for SMTP server to be ready
wait_for_smtp() {
echo "β³ Waiting for SMTP server at smtp-dev:1025..."
for i in {1..30}; do
if nc -z smtp-dev 1025 2>/dev/null; then
echo "β
SMTP server is ready"
return 0
fi
echo " Attempt $i/30: SMTP server not ready, waiting..."
sleep 2
done
echo "β SMTP server not available after 60 seconds"
return 1
}
# Wait for Traefik to be available
wait_for_traefik
# Wait for SMTP server to be available
wait_for_smtp
# Set up port forwarding from localhost:18273 to traefik:80
if ! nc -z localhost 18273 2>/dev/null; then
echo "π Setting up Traefik port forwarding: localhost:18273 β traefik:80"
socat TCP-LISTEN:18273,fork,reuseaddr TCP:traefik:80 &
TRAEFIK_SOCAT_PID=$!
else
echo "β
Traefik port forwarding already active: localhost:18273"
TRAEFIK_SOCAT_PID=""
fi
# Set up port forwarding from localhost:1025 to smtp-dev:1025
if ! nc -z localhost 1025 2>/dev/null; then
echo "π Setting up SMTP port forwarding: localhost:1025 β smtp-dev:1025"
socat TCP-LISTEN:1025,fork,reuseaddr TCP:smtp-dev:1025 &
SMTP_SOCAT_PID=$!
else
echo "β
SMTP port forwarding already active: localhost:1025"
SMTP_SOCAT_PID=""
fi
# Give socat a moment to start
sleep 2
# Test the port forwarding
echo "π§ͺ Testing port forwarding..."
if nc -z localhost 18273 2>/dev/null; then
echo "β
Traefik port forwarding established successfully"
else
echo "β Traefik port forwarding failed to establish"
exit 1
fi
if nc -z localhost 1025 2>/dev/null; then
echo "β
SMTP port forwarding established successfully"
else
echo "β SMTP port forwarding failed to establish"
exit 1
fi
# Function to cleanup on exit
cleanup() {
echo "π§Ή Cleaning up port forwarding..."
[ -n "$TRAEFIK_SOCAT_PID" ] && kill $TRAEFIK_SOCAT_PID 2>/dev/null
[ -n "$SMTP_SOCAT_PID" ] && kill $SMTP_SOCAT_PID 2>/dev/null
exit
}
# Set up signal handlers
trap cleanup SIGTERM SIGINT
echo "π Starting Phoenix Server..."
echo " π Phoenix: http://localhost:18273/phoenix"
echo " π§ SMTP Web UI: http://localhost:18273/mail"
echo ""
# Start Phoenix with debugger always enabled
echo "π Starting Phoenix with debugger on port 5678"
echo "π Connect your debugger to localhost:5678"
# Pass through any command line arguments (--dev must come before serve)
exec python -m debugpy --listen 0.0.0.0:5678 -m phoenix.server.main "$@" serve