#!/bin/bash
# 统一启动脚本 - 根据环境变量自动配置

# 默认配置
HOST=${HOST:-0.0.0.0}
PORT=${PORT:-8000}
WORKERS=${WORKERS:-1}
RELOAD=${RELOAD:-false}
TIMEOUT_KEEP_ALIVE=${TIMEOUT_KEEP_ALIVE:-600}

echo "=============================================="
echo "  KaoPeiLian Backend Starting..."
echo "=============================================="
echo "  HOST: $HOST"
echo "  PORT: $PORT"
echo "  WORKERS: $WORKERS"
echo "  RELOAD: $RELOAD"
echo "  TIMEOUT_KEEP_ALIVE: $TIMEOUT_KEEP_ALIVE"
echo "=============================================="

# 构建启动命令
CMD="uvicorn app.main:app --host $HOST --port $PORT --timeout-keep-alive $TIMEOUT_KEEP_ALIVE"

if [ "$RELOAD" = "true" ]; then
    # 开发模式：启用热重载（不支持多workers）
    CMD="$CMD --reload --reload-dir /app/app"
    echo "Mode: Development (hot reload enabled)"
else
    # 生产模式：多workers
    CMD="$CMD --workers $WORKERS"
    echo "Mode: Production ($WORKERS workers)"
fi

echo ""
echo "Executing: $CMD"
echo ""

exec $CMD
