File size: 2,213 Bytes
f08f6c6
 
 
 
 
 
 
 
892ed24
 
857bebe
 
fe51676
892ed24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08f6c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
857bebe
892ed24
 
 
f08f6c6
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
set -e

# Initialize environment variables with defaults if not set
export PORT=${PORT:-7860}
export HOST=${HOST:-"0.0.0.0"}
export WORKERS=${WORKERS:-1}
export LOG_LEVEL=${LOG_LEVEL:-"info"}
export DATABASE_URL=${DATABASE_URL:-"sqlite:///app/storage/audiobooks.db"}

# Set Python path to include both app directories
export PYTHONPATH="/app:/app/app:${PYTHONPATH:-}"

# Create storage directories if they don't exist
mkdir -p /app/storage/audio
mkdir -p /app/storage/text
mkdir -p /app/storage/temp

# Initialize database if using SQLite
if [[ "$DATABASE_URL" == sqlite* ]]; then
    echo "Using SQLite database at: $DATABASE_URL"
    # Create the database directory if it doesn't exist
    DB_DIR=$(dirname "${DATABASE_URL#sqlite:///}")
    mkdir -p "$DB_DIR"
    
    # Initialize alembic if not already initialized
    if [ ! -d "migrations" ]; then
        echo "Initializing alembic..."
        alembic init migrations
    fi
    
    # Create initial migration if none exists
    if [ ! -d "migrations/versions" ] || [ -z "$(ls -A migrations/versions)" ]; then
        echo "Creating initial migration..."
        alembic revision --autogenerate -m "Initial migration"
    fi
fi

# Wait for database to be ready (if using external database)
if [ ! -z "$DATABASE_URL" ] && [[ "$DATABASE_URL" != sqlite* ]]; then
    host=$(echo $DATABASE_URL | awk -F[@//] '{print $4}' | cut -d: -f1)
    port=$(echo $DATABASE_URL | awk -F[@//] '{print $4}' | cut -d: -f2 | cut -d/ -f1)
    
    echo "Waiting for database at $host:$port..."
    while ! nc -z $host $port; do
        sleep 1
    done
    echo "Database is ready!"
fi

# Run database migrations
echo "Running database migrations..."
PYTHONPATH="/app:/app/app:${PYTHONPATH:-}" alembic upgrade head || {
    echo "Warning: Database migration failed. This might be expected for first run."
    echo "Continuing with application startup..."
}

echo "Starting CSM-1B TTS API server..."
echo "Host: $HOST"
echo "Port: $PORT"
echo "Workers: $WORKERS"
echo "Log Level: $LOG_LEVEL"

# Start the FastAPI application using uvicorn
exec uvicorn app.main:app \
    --host "$HOST" \
    --port "$PORT" \
    --workers "$WORKERS" \
    --log-level "$LOG_LEVEL"