File size: 7,411 Bytes
09fa60b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# AudioForge Development Startup Script
# Starts PostgreSQL/Redis, initializes DB, then starts backend and frontend

param(
    [switch]$SkipServices = $false,
    [switch]$Help = $false
)

$Colors = @{
    Red = "Red"
    Green = "Green"
    Yellow = "Yellow"
    Blue = "Cyan"
    Cyan = "Cyan"
}

function Write-Info { Write-Host "[INFO] $args" -ForegroundColor $Colors.Blue }
function Write-Success { Write-Host "[SUCCESS] $args" -ForegroundColor $Colors.Green }
function Write-Warning { Write-Host "[WARNING] $args" -ForegroundColor $Colors.Yellow }
function Write-Error { Write-Host "[ERROR] $args" -ForegroundColor $Colors.Red }

if ($Help) {
    Write-Host @"

AudioForge Development Startup Script



Usage:

    .\scripts\dev_start.ps1 [-SkipServices] [-Help]



Options:

    -SkipServices    Skip starting PostgreSQL/Redis (assume they're already running)

    -Help           Show this help message



This script will:

1. Start PostgreSQL and Redis (if not skipped)

2. Wait for services to be ready

3. Initialize the database

4. Start backend server in background

5. Start frontend server in background

"@
    exit 0
}

$ProjectRoot = Split-Path -Parent $PSScriptRoot
Set-Location $ProjectRoot

# Step 1: Start PostgreSQL and Redis
if (-not $SkipServices) {
    Write-Info "Starting PostgreSQL and Redis..."
    
    # Check if Docker is available and running
    try {
        $null = docker ps 2>&1 | Out-Null
        if ($LASTEXITCODE -ne 0) {
            Write-Error "Docker Desktop is not running!"
            Write-Warning "Please start Docker Desktop and try again"
            Write-Info "You can skip this step with -SkipServices if services are already running"
            exit 1
        }
    } catch {
        Write-Error "Cannot connect to Docker daemon!"
        Write-Warning "Please start Docker Desktop and try again"
        Write-Info "You can skip this step with -SkipServices if services are already running"
        exit 1
    }
    
    # Start only postgres and redis services
    Write-Info "Starting PostgreSQL and Redis containers..."
    docker-compose up -d postgres redis
    
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Failed to start services!"
        Write-Info "Make sure Docker Desktop is running"
        exit 1
    }
    
    Write-Info "Waiting for PostgreSQL to be ready..."
    $maxAttempts = 30
    $attempt = 0
    $postgresReady = $false
    
    while ($attempt -lt $maxAttempts -and -not $postgresReady) {
        try {
            $null = docker exec audioforge-postgres pg_isready -U postgres 2>&1
            if ($LASTEXITCODE -eq 0) {
                $postgresReady = $true
                Write-Success "PostgreSQL is ready!"
            }
        } catch {
            # Continue waiting
        }
        
        if (-not $postgresReady) {
            Start-Sleep -Seconds 1
            $attempt++
            Write-Host "." -NoNewline
        }
    }
    
    Write-Host ""
    
    if (-not $postgresReady) {
        Write-Error "PostgreSQL failed to start within timeout!"
        exit 1
    }
    
    Write-Info "Waiting for Redis to be ready..."
    Start-Sleep -Seconds 2
    
    try {
        $null = docker exec audioforge-redis redis-cli ping 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Success "Redis is ready!"
        }
    } catch {
        Write-Warning "Redis health check failed, but continuing..."
    }
} else {
    Write-Info "Skipping service startup (assuming PostgreSQL/Redis are running)"
}

# Step 2: Setup Backend Environment
Write-Info "Setting up backend environment..."
Set-Location "$ProjectRoot\backend"

# Check for Python 3.11 venv first (for ML support), then fall back to regular venv
$venvPath = if (Test-Path ".venv311") { ".venv311" } elseif (Test-Path ".venv") { ".venv" } else { $null }

if ($null -eq $venvPath) {
    Write-Warning "Virtual environment not found! Creating .venv..."
    python -m venv .venv
    $venvPath = ".venv"
    & "$venvPath\Scripts\Activate.ps1"
    pip install -e ".[dev]"
} else {
    Write-Info "Using virtual environment: $venvPath"
    # Activate venv for subsequent commands
    & "$venvPath\Scripts\Activate.ps1"
}

# Step 3: Initialize Database
Write-Info "Initializing database..."

# Check if .env exists
if (-not (Test-Path ".env")) {
    Write-Warning ".env file not found! Running setup_env.py..."
    Set-Location $ProjectRoot
    python scripts/setup_env.py
    Set-Location "$ProjectRoot\backend"
}

# Use venv's Python explicitly for database initialization
& "$venvPath\Scripts\python.exe" scripts/init_db.py

if ($LASTEXITCODE -ne 0) {
    Write-Error "Database initialization failed!"
    Write-Info "Make sure PostgreSQL is running and DATABASE_URL is correct in backend/.env"
    exit 1
}

Write-Success "Database initialized successfully!"

# Step 4: Start Backend
Write-Info "Starting backend server..."

# Start backend in new window
$backendJob = Start-Process powershell -ArgumentList @(
    "-NoExit",
    "-Command",
    "cd '$ProjectRoot\backend'; `$venv = if (Test-Path .venv311) { '.venv311' } elseif (Test-Path .venv) { '.venv' } else { `$null }; if (`$venv) { & `"`$venv\Scripts\Activate.ps1`" }; uvicorn app.main:app --reload"
) -PassThru

Write-Success "Backend starting in new window (PID: $($backendJob.Id))"
Write-Info "Backend will be available at http://localhost:8000"

# Step 5: Start Frontend
Write-Info "Starting frontend server..."
Set-Location "$ProjectRoot\frontend"

# Check if node_modules exists
if (-not (Test-Path "node_modules")) {
    Write-Warning "Frontend dependencies not installed! Installing..."
    pnpm install
}

# Ensure .env.local exists
if (-not (Test-Path ".env.local")) {
    Write-Info "Creating frontend .env.local..."
    "NEXT_PUBLIC_API_URL=http://localhost:8000" | Out-File -FilePath ".env.local" -Encoding UTF8
}

# Start frontend in new window
$frontendJob = Start-Process powershell -ArgumentList @(
    "-NoExit",
    "-Command",
    "cd '$ProjectRoot\frontend'; pnpm dev"
) -PassThru

Write-Success "Frontend starting in new window (PID: $($frontendJob.Id))"
Write-Info "Frontend will be available at http://localhost:3000"

# Wait a bit for services to start
Write-Info "Waiting for services to initialize..."
Start-Sleep -Seconds 5

# Final status
Write-Host ""
Write-Host "================================================================" -ForegroundColor Green
Write-Host "                                                               " -ForegroundColor Green
Write-Host "      Development Environment Started!                         " -ForegroundColor Green
Write-Host "                                                               " -ForegroundColor Green
Write-Host "================================================================" -ForegroundColor Green
Write-Host ""
Write-Success "Backend:  http://localhost:8000"
Write-Success "Frontend: http://localhost:3000"
Write-Success "API Docs: http://localhost:8000/docs"
Write-Host ""
Write-Info "Backend and Frontend are running in separate PowerShell windows"
Write-Info "Close those windows to stop the servers"
Write-Host ""
Write-Info "To stop PostgreSQL/Redis: docker-compose down"
Write-Host ""