#!/bin/bash set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color echo -e "${GREEN}Starting Foxhunt Services...${NC}" # Create logs and runtime directories mkdir -p logs mkdir -p /tmp/foxhunt # Setup TLS certificates (required by backtesting and ML services) if [ ! -f /tmp/foxhunt/certs/server.crt ]; then echo -e "${YELLOW}Generating self-signed TLS certificates for development...${NC}" mkdir -p /tmp/foxhunt/certs openssl req -x509 -newkey rsa:4096 -keyout /tmp/foxhunt/certs/server.key -out /tmp/foxhunt/certs/server.crt -days 365 -nodes -subj "/CN=localhost" >/dev/null 2>&1 echo -e "${GREEN}TLS certificates generated${NC}" fi # Load environment variables (using existing test database) export POSTGRES_HOST=localhost export POSTGRES_PORT=5433 export POSTGRES_USER=foxhunt_test export POSTGRES_PASSWORD=test_password export POSTGRES_DB=foxhunt_test export DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}" export REDIS_HOST=localhost export REDIS_PORT=6380 export REDIS_URL="redis://${REDIS_HOST}:${REDIS_PORT}" export VAULT_ADDR="http://localhost:8200" export VAULT_TOKEN="foxhunt_vault_token_change_in_prod" export RUST_LOG=info,api_gateway=debug,trading_service=debug export RUST_BACKTRACE=1 # Generate a proper JWT secret with high entropy (base64 encoded random bytes = uppercase + lowercase + numbers + symbols) export JWT_SECRET=$(openssl rand -base64 64 | tr -d '\n') export JWT_EXPIRY_SECONDS=3600 # TLS certificates path export TLS_CERT_PATH="/tmp/foxhunt/certs/server.crt" export TLS_KEY_PATH="/tmp/foxhunt/certs/server.key" # API keys (using dummy values for now - services should handle missing keys gracefully) export BENZINGA_API_KEY="dummy_api_key_for_testing" export DATABENTO_API_KEY="dummy_api_key_for_testing" # Service ports export API_GATEWAY_PORT=50050 export TRADING_SERVICE_PORT=50051 export BACKTESTING_SERVICE_PORT=50052 export ML_TRAINING_SERVICE_PORT=50053 # Backend service URLs (for API Gateway) export TRADING_SERVICE_URL="http://localhost:${TRADING_SERVICE_PORT}" export BACKTESTING_SERVICE_URL="http://localhost:${BACKTESTING_SERVICE_PORT}" export ML_TRAINING_SERVICE_URL="http://localhost:${ML_TRAINING_SERVICE_PORT}" # Kill switch socket path (writable location) export KILL_SWITCH_SOCKET_PATH="/tmp/foxhunt/kill_switch.sock" echo -e "${YELLOW}Environment configured${NC}" echo "DATABASE_URL: ${DATABASE_URL}" echo "REDIS_URL: ${REDIS_URL}" echo "VAULT_ADDR: ${VAULT_ADDR}" echo "KILL_SWITCH_SOCKET_PATH: ${KILL_SWITCH_SOCKET_PATH}" echo "TLS_CERT_PATH: ${TLS_CERT_PATH}" echo "JWT_SECRET length: ${#JWT_SECRET} chars (base64 encoded for high entropy)" # Start Vault if not running if ! docker ps | grep -q foxhunt-vault; then echo -e "${YELLOW}Starting Vault...${NC}" # Remove old container if exists docker rm -f foxhunt-vault 2>/dev/null || true docker run -d --name foxhunt-vault \ -p 8200:8200 \ -e VAULT_DEV_ROOT_TOKEN_ID="${VAULT_TOKEN}" \ -e VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200 \ hashicorp/vault:latest server -dev >/dev/null 2>&1 sleep 3 echo -e "${GREEN}Vault started${NC}" else echo -e "${GREEN}Vault already running${NC}" fi echo -e "${YELLOW}Starting backend services first (they must be ready before API Gateway)...${NC}" # Start Trading Service echo -e "${YELLOW}Starting Trading Service (port ${TRADING_SERVICE_PORT})...${NC}" ./target/release/trading_service &> logs/trading_service.log & TRADING_SERVICE_PID=$! echo "Trading Service PID: ${TRADING_SERVICE_PID}" echo "${TRADING_SERVICE_PID}" > logs/trading_service.pid # Start Backtesting Service echo -e "${YELLOW}Starting Backtesting Service (port ${BACKTESTING_SERVICE_PORT})...${NC}" ./target/release/backtesting_service &> logs/backtesting_service.log & BACKTESTING_SERVICE_PID=$! echo "Backtesting Service PID: ${BACKTESTING_SERVICE_PID}" echo "${BACKTESTING_SERVICE_PID}" > logs/backtesting_service.pid # Start ML Training Service (with "serve" command) echo -e "${YELLOW}Starting ML Training Service (port ${ML_TRAINING_SERVICE_PORT})...${NC}" ./target/release/ml_training_service serve &> logs/ml_training_service.log & ML_TRAINING_SERVICE_PID=$! echo "ML Training Service PID: ${ML_TRAINING_SERVICE_PID}" echo "${ML_TRAINING_SERVICE_PID}" > logs/ml_training_service.pid echo -e "${YELLOW}Waiting 15 seconds for backend services to start...${NC}" sleep 15 # Check backend services are responding echo -e "${YELLOW}Checking backend service health...${NC}" BACKENDS_READY=true for port in ${TRADING_SERVICE_PORT} ${BACKTESTING_SERVICE_PORT} ${ML_TRAINING_SERVICE_PORT}; do if nc -z localhost ${port} 2>/dev/null; then echo -e "${GREEN}✓${NC} Port ${port} is listening" else echo -e "${RED}✗${NC} Port ${port} is NOT listening" BACKENDS_READY=false fi done if [ "$BACKENDS_READY" = false ]; then echo -e "${YELLOW}Some backend services failed to start. Checking logs...${NC}" echo "" echo "=== Trading Service Log (last 30 lines) ===" tail -30 logs/trading_service.log echo "" echo "=== Backtesting Service Log (last 30 lines) ===" tail -30 logs/backtesting_service.log echo "" echo "=== ML Training Service Log (last 30 lines) ===" tail -30 logs/ml_training_service.log echo "" echo -e "${RED}Backend services not ready. Exiting.${NC}" exit 1 fi # Now start API Gateway echo -e "${YELLOW}Starting API Gateway (port ${API_GATEWAY_PORT})...${NC}" ./target/release/api_gateway &> logs/api_gateway.log & API_GATEWAY_PID=$! echo "API Gateway PID: ${API_GATEWAY_PID}" echo "${API_GATEWAY_PID}" > logs/api_gateway.pid echo -e "${GREEN}All services started!${NC}" echo "" echo "Service PIDs:" echo " Trading Service: ${TRADING_SERVICE_PID}" echo " Backtesting Service: ${BACKTESTING_SERVICE_PID}" echo " ML Training Service: ${ML_TRAINING_SERVICE_PID}" echo " API Gateway: ${API_GATEWAY_PID}" echo "" echo -e "${YELLOW}Waiting 10 seconds for API Gateway to initialize...${NC}" sleep 10 # Health check echo -e "${GREEN}Performing final health checks...${NC}" echo "" # Function to check if port is listening check_port() { local port=$1 local service=$2 if nc -z localhost ${port} 2>/dev/null; then echo -e "${GREEN}✓${NC} ${service} (port ${port}): LISTENING" return 0 else echo -e "${RED}✗${NC} ${service} (port ${port}): NOT RESPONDING" return 1 fi } # Check all services ALL_HEALTHY=true check_port 50051 "Trading Service" || ALL_HEALTHY=false check_port 50052 "Backtesting Service" || ALL_HEALTHY=false check_port 50053 "ML Training Service" || ALL_HEALTHY=false check_port 50050 "API Gateway" || ALL_HEALTHY=false echo "" if [ "$ALL_HEALTHY" = true ]; then echo -e "${GREEN}✓✓✓ All services are healthy and ready for load testing! ✓✓✓${NC}" echo "" echo "Service URLs:" echo " API Gateway: http://localhost:50050" echo " Trading Service: http://localhost:50051" echo " Backtesting Service: http://localhost:50052" echo " ML Training Service: http://localhost:50053" else echo -e "${YELLOW}Some services may have issues. Check logs in ./logs/${NC}" fi echo "" echo "To stop all services, run: ./stop_services.sh" echo "To view logs: tail -f logs/*.log" echo "To check service status: ps aux | grep -E 'trading_service|backtesting_service|ml_training_service|api_gateway'"