#!/bin/bash
set -e

# 等待数据库服务就绪
echo "等待数据库服务就绪..."
until nc -z ${DB_HOST} ${DB_PORT}; do
  echo "数据库服务未就绪 - 等待..."
  sleep 2
done
echo "数据库服务已就绪!"

# 等待Redis服务就绪
echo "等待Redis服务就绪..."
until nc -z ${REDIS_HOST} ${REDIS_PORT}; do
  echo "Redis服务未就绪 - 等待..."
  sleep 2
done
echo "Redis服务已就绪!"

# 初始化数据库（如果需要）
echo "初始化数据库..."
python -c "from app import init_database; init_database()"

# 启动应用
echo "启动应用..."
exec "$@" 