# HM ITAM Server Start Script [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 function Get-EnvValue { param( [string]$FilePath, [string]$Key ) if (-not (Test-Path $FilePath)) { return $null } $line = Get-Content $FilePath | Where-Object { $_ -match "^$Key=" } | Select-Object -First 1 if (-not $line) { return $null } return ($line -split '=', 2)[1].Trim() } function Test-TcpPortFast { param( [string]$HostName, [int]$Port, [int]$TimeoutMs = 3000 ) $client = New-Object System.Net.Sockets.TcpClient try { $asyncResult = $client.BeginConnect($HostName, $Port, $null, $null) if (-not $asyncResult.AsyncWaitHandle.WaitOne($TimeoutMs, $false)) { $client.Close() return $false } $client.EndConnect($asyncResult) $client.Close() return $true } catch { $client.Close() return $false } } Write-Host "============================================" -ForegroundColor Cyan Write-Host " HM ITAM System Start" -ForegroundColor Cyan Write-Host "============================================" -ForegroundColor Cyan Write-Host "" Write-Host "[INFO] Checking Node.js and npm..." if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Write-Host "[ERROR] Node.js not found." -ForegroundColor Red Read-Host "Press Enter to exit" exit } if (-not (Test-Path "node_modules")) { Write-Host "[INFO] Installing dependencies..." npm install } Write-Host "[INFO] Checking ports..." $backendPort = 3000 $frontendPort = 8080 $envFilePath = Join-Path $PSScriptRoot '.env' $dbHost = Get-EnvValue -FilePath $envFilePath -Key 'DB_HOST' $dbPort = Get-EnvValue -FilePath $envFilePath -Key 'DB_PORT' if (-not $dbPort) { $dbPort = '3306' } if (Get-NetTCPConnection -LocalPort $backendPort -ErrorAction SilentlyContinue) { Write-Host "[WARNING] Port $backendPort [Backend] is already in use." -ForegroundColor Yellow } if (Get-NetTCPConnection -LocalPort $frontendPort -ErrorAction SilentlyContinue) { Write-Host "[WARNING] Port $frontendPort [Frontend] is already in use." -ForegroundColor Yellow } if (-not $dbHost) { Write-Host "[WARNING] .env is missing DB_HOST. Backend and frontend will still start, but DB calls will fail until DB settings are fixed." -ForegroundColor Yellow } else { Write-Host "[INFO] Checking external DB reachability..." $dbReachable = Test-TcpPortFast -HostName $dbHost -Port ([int]$dbPort) if ($dbReachable) { Write-Host "[INFO] External DB reachable: $dbHost`:$dbPort" } else { Write-Host "[WARNING] External DB is unreachable: $dbHost`:$dbPort" -ForegroundColor Yellow Write-Host "[WARNING] Backend and frontend will still start, but DB-backed screens and APIs may fail." -ForegroundColor Yellow } } Write-Host "" Write-Host "[INFO] Starting Backend [Port: 3000]..." Start-Process cmd -ArgumentList "/k npm run server" Write-Host "[INFO] Starting Frontend [Port: 8080]..." Start-Process cmd -ArgumentList "/k npm run dev" Write-Host "" Write-Host "============================================" -ForegroundColor Green Write-Host " [OK] Server commands issued successfully." -ForegroundColor Green Write-Host " [INFO] Please check the new windows for logs." Write-Host "============================================" -ForegroundColor Green Write-Host "" Read-Host "Press Enter to continue..."