CoolFace
Apppublic

OnyxMunk/AudioForge

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
dev_start.ps1222 linesDownload Raw Back to scripts
1# AudioForge Development Startup Script
2# Starts PostgreSQL/Redis, initializes DB, then starts backend and frontend
3
4param(
5    [switch]$SkipServices = $false,
6    [switch]$Help = $false
7)
8
9$Colors = @{
10    Red = "Red"
11    Green = "Green"
12    Yellow = "Yellow"
13    Blue = "Cyan"
14    Cyan = "Cyan"
15}
16
17function Write-Info { Write-Host "[INFO] $args" -ForegroundColor $Colors.Blue }
18function Write-Success { Write-Host "[SUCCESS] $args" -ForegroundColor $Colors.Green }
19function Write-Warning { Write-Host "[WARNING] $args" -ForegroundColor $Colors.Yellow }
20function Write-Error { Write-Host "[ERROR] $args" -ForegroundColor $Colors.Red }
21
22if ($Help) {
23    Write-Host @"
24AudioForge Development Startup Script
25
26Usage:
27    .\scripts\dev_start.ps1 [-SkipServices] [-Help]
28
29Options:
30    -SkipServices    Skip starting PostgreSQL/Redis (assume they're already running)
31    -Help           Show this help message
32
33This script will:
341. Start PostgreSQL and Redis (if not skipped)
352. Wait for services to be ready
363. Initialize the database
374. Start backend server in background
385. Start frontend server in background
39"@
40    exit 0
41}
42
43$ProjectRoot = Split-Path -Parent $PSScriptRoot
44Set-Location $ProjectRoot
45
46# Step 1: Start PostgreSQL and Redis
47if (-not $SkipServices) {
48    Write-Info "Starting PostgreSQL and Redis..."
49    
50    # Check if Docker is available and running
51    try {
52        $null = docker ps 2>&1 | Out-Null
53        if ($LASTEXITCODE -ne 0) {
54            Write-Error "Docker Desktop is not running!"
55            Write-Warning "Please start Docker Desktop and try again"
56            Write-Info "You can skip this step with -SkipServices if services are already running"
57            exit 1
58        }
59    } catch {
60        Write-Error "Cannot connect to Docker daemon!"
61        Write-Warning "Please start Docker Desktop and try again"
62        Write-Info "You can skip this step with -SkipServices if services are already running"
63        exit 1
64    }
65    
66    # Start only postgres and redis services
67    Write-Info "Starting PostgreSQL and Redis containers..."
68    docker-compose up -d postgres redis
69    
70    if ($LASTEXITCODE -ne 0) {
71        Write-Error "Failed to start services!"
72        Write-Info "Make sure Docker Desktop is running"
73        exit 1
74    }
75    
76    Write-Info "Waiting for PostgreSQL to be ready..."
77    $maxAttempts = 30
78    $attempt = 0
79    $postgresReady = $false
80    
81    while ($attempt -lt $maxAttempts -and -not $postgresReady) {
82        try {
83            $null = docker exec audioforge-postgres pg_isready -U postgres 2>&1
84            if ($LASTEXITCODE -eq 0) {
85                $postgresReady = $true
86                Write-Success "PostgreSQL is ready!"
87            }
88        } catch {
89            # Continue waiting
90        }
91        
92        if (-not $postgresReady) {
93            Start-Sleep -Seconds 1
94            $attempt++
95            Write-Host "." -NoNewline
96        }
97    }
98    
99    Write-Host ""
100    
101    if (-not $postgresReady) {
102        Write-Error "PostgreSQL failed to start within timeout!"
103        exit 1
104    }
105    
106    Write-Info "Waiting for Redis to be ready..."
107    Start-Sleep -Seconds 2
108    
109    try {
110        $null = docker exec audioforge-redis redis-cli ping 2>&1
111        if ($LASTEXITCODE -eq 0) {
112            Write-Success "Redis is ready!"
113        }
114    } catch {
115        Write-Warning "Redis health check failed, but continuing..."
116    }
117} else {
118    Write-Info "Skipping service startup (assuming PostgreSQL/Redis are running)"
119}
120
121# Step 2: Setup Backend Environment
122Write-Info "Setting up backend environment..."
123Set-Location "$ProjectRoot\backend"
124
125# Check for Python 3.11 venv first (for ML support), then fall back to regular venv
126$venvPath = if (Test-Path ".venv311") { ".venv311" } elseif (Test-Path ".venv") { ".venv" } else { $null }
127
128if ($null -eq $venvPath) {
129    Write-Warning "Virtual environment not found! Creating .venv..."
130    python -m venv .venv
131    $venvPath = ".venv"
132    & "$venvPath\Scripts\Activate.ps1"
133    pip install -e ".[dev]"
134} else {
135    Write-Info "Using virtual environment: $venvPath"
136    # Activate venv for subsequent commands
137    & "$venvPath\Scripts\Activate.ps1"
138}
139
140# Step 3: Initialize Database
141Write-Info "Initializing database..."
142
143# Check if .env exists
144if (-not (Test-Path ".env")) {
145    Write-Warning ".env file not found! Running setup_env.py..."
146    Set-Location $ProjectRoot
147    python scripts/setup_env.py
148    Set-Location "$ProjectRoot\backend"
149}
150
151# Use venv's Python explicitly for database initialization
152& "$venvPath\Scripts\python.exe" scripts/init_db.py
153
154if ($LASTEXITCODE -ne 0) {
155    Write-Error "Database initialization failed!"
156    Write-Info "Make sure PostgreSQL is running and DATABASE_URL is correct in backend/.env"
157    exit 1
158}
159
160Write-Success "Database initialized successfully!"
161
162# Step 4: Start Backend
163Write-Info "Starting backend server..."
164
165# Start backend in new window
166$backendJob = Start-Process powershell -ArgumentList @(
167    "-NoExit",
168    "-Command",
169    "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"
170) -PassThru
171
172Write-Success "Backend starting in new window (PID: $($backendJob.Id))"
173Write-Info "Backend will be available at http://localhost:8000"
174
175# Step 5: Start Frontend
176Write-Info "Starting frontend server..."
177Set-Location "$ProjectRoot\frontend"
178
179# Check if node_modules exists
180if (-not (Test-Path "node_modules")) {
181    Write-Warning "Frontend dependencies not installed! Installing..."
182    pnpm install
183}
184
185# Ensure .env.local exists
186if (-not (Test-Path ".env.local")) {
187    Write-Info "Creating frontend .env.local..."
188    "NEXT_PUBLIC_API_URL=http://localhost:8000" | Out-File -FilePath ".env.local" -Encoding UTF8
189}
190
191# Start frontend in new window
192$frontendJob = Start-Process powershell -ArgumentList @(
193    "-NoExit",
194    "-Command",
195    "cd '$ProjectRoot\frontend'; pnpm dev"
196) -PassThru
197
198Write-Success "Frontend starting in new window (PID: $($frontendJob.Id))"
199Write-Info "Frontend will be available at http://localhost:3000"
200
201# Wait a bit for services to start
202Write-Info "Waiting for services to initialize..."
203Start-Sleep -Seconds 5
204
205# Final status
206Write-Host ""
207Write-Host "================================================================" -ForegroundColor Green
208Write-Host "                                                               " -ForegroundColor Green
209Write-Host "      Development Environment Started!                         " -ForegroundColor Green
210Write-Host "                                                               " -ForegroundColor Green
211Write-Host "================================================================" -ForegroundColor Green
212Write-Host ""
213Write-Success "Backend:  http://localhost:8000"
214Write-Success "Frontend: http://localhost:3000"
215Write-Success "API Docs: http://localhost:8000/docs"
216Write-Host ""
217Write-Info "Backend and Frontend are running in separate PowerShell windows"
218Write-Info "Close those windows to stop the servers"
219Write-Host ""
220Write-Info "To stop PostgreSQL/Redis: docker-compose down"
221Write-Host ""
222