#!/bin/bash # Check MockAPI port configuration consistency # Run this to verify port settings are correct echo "=== MockAPI Port Configuration Check ===" echo APP_DIR="/opt/mockapi" ENV_FILE="${APP_DIR}/.env" SERVICE_FILE="/etc/systemd/system/mockapi.service" # Colors GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' NC='\033[0m' # No Color print_ok() { echo -e "${GREEN}[✓]${NC} $1" } print_warning() { echo -e "${YELLOW}[!]${NC} $1" } print_error() { echo -e "${RED}[✗]${NC} $1" } # Check if files exist if [[ ! -f "$ENV_FILE" ]]; then print_error ".env file not found: $ENV_FILE" exit 1 fi if [[ ! -f "$SERVICE_FILE" ]]; then print_error "Service file not found: $SERVICE_FILE" exit 1 fi print_ok "Checking configuration files..." # Read values from .env ENV_PORT=$(grep "^PORT=" "$ENV_FILE" | cut -d'=' -f2 || echo "NOT_FOUND") ENV_HOST=$(grep "^HOST=" "$ENV_FILE" | cut -d'=' -f2 || echo "NOT_FOUND") ENV_ISSUER=$(grep "^OAUTH2_ISSUER=" "$ENV_FILE" | cut -d'=' -f2 || echo "NOT_FOUND") # Read from service file SERVICE_LINE=$(grep "^ExecStart=" "$SERVICE_FILE" || echo "NOT_FOUND") if [[ "$SERVICE_LINE" != "NOT_FOUND" ]]; then # Extract host and port from ExecStart SERVICE_HOST=$(echo "$SERVICE_LINE" | grep -o -- '--host=[^ ]*' | cut -d'=' -f2 || echo "NOT_FOUND") SERVICE_PORT=$(echo "$SERVICE_LINE" | grep -o -- '--port=[^ ]*' | cut -d'=' -f2 || echo "NOT_FOUND") else SERVICE_HOST="NOT_FOUND" SERVICE_PORT="NOT_FOUND" fi echo echo "=== Current Configuration ===" echo echo ".env file ($ENV_FILE):" echo " PORT: $ENV_PORT" echo " HOST: $ENV_HOST" echo " OAUTH2_ISSUER: $ENV_ISSUER" echo echo "Service file ($SERVICE_FILE):" echo " HOST in ExecStart: $SERVICE_HOST" echo " PORT in ExecStart: $SERVICE_PORT" echo echo "=== Analysis ===" echo # Check 1: Port numeric validation if [[ "$ENV_PORT" =~ ^[0-9]+$ ]]; then print_ok "PORT in .env is numeric: $ENV_PORT" else print_error "PORT in .env is not numeric: '$ENV_PORT'" fi if [[ "$SERVICE_PORT" =~ ^[0-9]+$ ]]; then print_ok "PORT in service file is numeric: $SERVICE_PORT" else print_error "PORT in service file is not numeric: '$SERVICE_PORT'" fi # Check 2: Port consistency if [[ "$ENV_PORT" == "$SERVICE_PORT" ]]; then print_ok "Ports match: .env=$ENV_PORT, service=$SERVICE_PORT" else print_error "Port mismatch! .env=$ENV_PORT, service=$SERVICE_PORT" fi # Check 3: Host consistency if [[ "$ENV_HOST" == "$SERVICE_HOST" ]]; then print_ok "Hosts match: .env=$ENV_HOST, service=$SERVICE_HOST" elif [[ "$ENV_HOST" == "NOT_FOUND" ]] || [[ "$SERVICE_HOST" == "NOT_FOUND" ]]; then print_warning "Host not found in one or both files" else print_error "Host mismatch! .env=$ENV_HOST, service=$SERVICE_HOST" fi # Check 4: OAUTH2_ISSUER consistency EXPECTED_ISSUER="http://localhost:${ENV_PORT}" if [[ "$ENV_ISSUER" == "$EXPECTED_ISSUER" ]]; then print_ok "OAUTH2_ISSUER matches port: $ENV_ISSUER" elif [[ "$ENV_ISSUER" == "NOT_FOUND" ]]; then print_warning "OAUTH2_ISSUER not found in .env" else print_warning "OAUTH2_ISSUER doesn't match port: $ENV_ISSUER (expected: $EXPECTED_ISSUER)" fi # Check 5: Service status echo echo "=== Service Status ===" echo if systemctl is-active --quiet mockapi 2>/dev/null; then print_ok "Service is running" # Try to get actual listening port if command -v ss &> /dev/null; then LISTENING_PORT=$(ss -tuln | grep ":$ENV_PORT " | head -1 | awk '{print $4}' | cut -d':' -f2) if [[ -n "$LISTENING_PORT" ]]; then print_ok "Service is listening on port: $LISTENING_PORT" if [[ "$LISTENING_PORT" == "$ENV_PORT" ]]; then print_ok "Listening port matches configured port" else print_error "Listening port ($LISTENING_PORT) doesn't match configured port ($ENV_PORT)" fi fi fi # Test health endpoint echo echo "Testing health endpoint..." if curl -f -s --max-time 5 http://localhost:${ENV_PORT}/health > /dev/null 2>&1; then print_ok "Health check passed: http://localhost:${ENV_PORT}/health" else print_warning "Health check failed on port $ENV_PORT" print_warning "Service might be starting up or have issues" fi else print_error "Service is not running" echo "Status:" systemctl status mockapi --no-pager -l | head -20 fi echo echo "=== Recommendations ===" echo if [[ "$ENV_PORT" != "$SERVICE_PORT" ]]; then echo "1. Fix port mismatch:" echo " Run: sudo bash change_port.sh" echo " Or manually update .env and service file to match" fi if [[ ! "$ENV_PORT" =~ ^[0-9]+$ ]]; then echo "2. Fix invalid PORT in .env:" echo " Edit $ENV_FILE and set PORT to a numeric value (e.g., 8000)" fi if [[ "$ENV_ISSUER" != "http://localhost:${ENV_PORT}" ]] && [[ "$ENV_ISSUER" != "NOT_FOUND" ]]; then echo "3. Update OAUTH2_ISSUER to match port:" echo " Edit $ENV_FILE and set: OAUTH2_ISSUER=http://localhost:${ENV_PORT}" fi if ! systemctl is-active --quiet mockapi 2>/dev/null; then echo "4. Start the service:" echo " sudo systemctl start mockapi" echo " sudo systemctl status mockapi" fi echo echo "=== Summary ===" echo "Run 'sudo bash change_port.sh' to fix any configuration issues." echo "Run 'sudo systemctl restart mockapi' after making changes."