Files
cryptowallet/start.bat
2026-04-14 13:30:26 +03:00

223 lines
8.3 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
echo.
echo ==========================================
echo CryptoWallet + BITOK Auth - Local Dev
echo ==========================================
echo.
:: ── 1. Check pnpm ────────────────────────────────────────────────────────────
where pnpm >nul 2>&1
if errorlevel 1 (
echo [ERROR] pnpm not found. Install with: npm install -g pnpm
pause
exit /b 1
)
:: ── 2. Check Docker ──────────────────────────────────────────────────────────
where docker >nul 2>&1
if errorlevel 1 (
echo [ERROR] Docker not found. Install Docker Desktop and ensure it is running.
pause
exit /b 1
)
:: ── 3. Stop local PostgreSQL (if any) so Docker gets port 5432 ─────────────────
echo [1/8] Preparing environment...
net stop postgresql-x64-16 2>nul
timeout /t 2 /nobreak >nul
:: ── 4. Wait for Docker Engine ─────────────────────────────────────────────────
echo Waiting for Docker Engine...
set /a docker_attempts=0
:waitdocker
set /a docker_attempts+=1
if !docker_attempts! gtr 30 (
echo [ERROR] Docker Engine did not respond after 60 seconds.
echo Make sure Docker Desktop is fully started ^(whale icon in tray^).
pause
exit /b 1
)
docker info >nul 2>&1
if errorlevel 1 (
timeout /t 2 /nobreak >nul
goto waitdocker
)
echo Docker ready.
:: ── 5. Start infrastructure via Docker Compose ────────────────────────────────
echo [2/8] Starting infrastructure (PostgreSQL, Vault, RabbitMQ, KeyDB)...
docker compose up -d postgres vault vault-init rabbitmq keydb 2>&1
if errorlevel 1 (
echo Retrying with docker-compose...
docker-compose up -d postgres vault vault-init rabbitmq keydb 2>&1
)
if errorlevel 1 (
echo.
echo [ERROR] Could not start Docker Compose.
echo.
echo Common fixes:
echo 1. Wait 30-60 sec after opening Docker Desktop
echo 2. Restart Docker Desktop ^(right-click tray icon -^> Restart^)
echo 3. Run: docker compose up -d postgres vault vault-init rabbitmq keydb
echo.
pause
exit /b 1
)
:: Wait for PostgreSQL to be ready
set /a pg_attempts=0
:waitpg
set /a pg_attempts+=1
if !pg_attempts! gtr 30 (
echo [ERROR] PostgreSQL not responding after 60 seconds.
pause
exit /b 1
)
timeout /t 2 /nobreak >nul
docker exec cryptowallet-db pg_isready -U postgres -q 2>nul
if errorlevel 1 goto waitpg
echo PostgreSQL is ready.
:: Wait for RabbitMQ to be ready
echo Waiting for RabbitMQ...
set /a rmq_attempts=0
:waitrmq
set /a rmq_attempts+=1
if !rmq_attempts! gtr 30 (
echo [WARN] RabbitMQ slow to start, continuing anyway...
goto rmq_done
)
timeout /t 2 /nobreak >nul
docker exec cryptowallet-rabbitmq rabbitmq-diagnostics check_port_connectivity >nul 2>&1
if errorlevel 1 goto waitrmq
:rmq_done
echo RabbitMQ is ready.
:: ── 6. Create databases ──────────────────────────────────────────────────────
echo [3/8] Ensuring databases...
docker exec cryptowallet-db psql -U postgres -c "CREATE DATABASE cryptowallet_devphase3" 2>nul
if errorlevel 1 (
echo cryptowallet_devphase3 may already exist, continuing...
)
docker exec cryptowallet-db psql -U postgres -c "CREATE DATABASE bitok_dev" 2>nul
if errorlevel 1 (
echo bitok_dev may already exist, continuing...
)
echo Databases ready.
:: ── 7. Wait for vault-init to finish ─────────────────────────────────────────
echo [4/8] Waiting for Vault initialization...
set /a vault_attempts=0
:waitvault
set /a vault_attempts+=1
if !vault_attempts! gtr 30 (
echo [WARN] vault-init taking long, continuing...
goto vault_done
)
timeout /t 2 /nobreak >nul
docker inspect cryptowallet-vault-init --format="{{.State.Status}}" 2>nul | findstr /C:"exited" >nul 2>&1
if errorlevel 1 goto waitvault
:vault_done
echo Vault initialized.
:: ── 8. Update .env ───────────────────────────────────────────────────────────
echo [5/8] Updating .env...
if not exist .env (
copy .env.example .env >nul
)
powershell -NoProfile -Command "(Get-Content .env) -replace '^DB_NAME=.*', 'DB_NAME=cryptowallet_devphase3' -replace '^DATABASE_URL=.*', 'DATABASE_URL=postgresql://postgres:postgres@localhost:5432/cryptowallet_devphase3' | Set-Content .env"
echo .env updated.
:: ── 9. Install deps and run migrations ────────────────────────────────────────
echo [6/8] Installing dependencies...
call pnpm install
if errorlevel 1 (
echo [ERROR] pnpm install failed.
pause
exit /b 1
)
echo Dependencies installed.
echo Running database migrations...
cd apps\api
call pnpm migrate
if errorlevel 1 (
echo Migration failed - resetting DB and retrying...
call pnpm db:reset
call pnpm migrate
)
if errorlevel 1 (
echo [ERROR] Migrations failed - check output above.
pause
exit /b 1
)
cd ..\..
echo Migrations done.
:: ── 10. Initialize BITOK database tables ──────────────────────────────────────
echo [7/8] Initializing BITOK database...
if exist BITOK\sql\tables.sql (
docker exec -i cryptowallet-db psql -U postgres -d bitok_dev < BITOK\sql\tables.sql 2>nul
echo BITOK tables initialized.
) else (
echo BITOK/sql/tables.sql not found, skipping...
)
:: ── 11. Start BITOK auth service (Docker) ─────────────────────────────────────
echo Starting BITOK auth service...
docker compose up -d bitok-auth 2>&1
if errorlevel 1 (
echo [WARN] BITOK auth container failed to start. Check: docker logs cryptowallet-bitok
)
:: Wait briefly for BITOK to be responsive
set /a bitok_attempts=0
:waitbitok
set /a bitok_attempts+=1
if !bitok_attempts! gtr 15 (
echo [WARN] BITOK slow to start, continuing...
goto bitok_done
)
timeout /t 2 /nobreak >nul
powershell -NoProfile -Command "try { $r = Invoke-WebRequest -Uri http://localhost:8000/ping -TimeoutSec 2 -UseBasicParsing; exit 0 } catch { exit 1 }" >nul 2>&1
if errorlevel 1 goto waitbitok
:bitok_done
echo BITOK auth service ready.
:: ── 12. Stop old dev servers and clear lock ─────────────────────────────────────
echo [8/8] Starting wallet servers...
powershell -NoProfile -Command "Get-NetTCPConnection -LocalPort 3000,3001 -ErrorAction SilentlyContinue | ForEach-Object { Stop-Process -Id $_.OwningProcess -Force -ErrorAction SilentlyContinue }"
if exist apps\web\.next\dev\lock del apps\web\.next\dev\lock 2>nul
timeout /t 1 /nobreak >nul
:: Start wallet API and Web
start "CryptoWallet API [port 3001]" cmd /k "cd /d %~dp0apps\api && pnpm dev"
timeout /t 2 /nobreak >nul
start "CryptoWallet Web [port 3000]" cmd /k "cd /d %~dp0apps\web && pnpm dev"
:: ── Done ─────────────────────────────────────────────────────────────────────
echo.
echo ==========================================
echo All services started!
echo.
echo Web: http://localhost:3000
echo API: http://localhost:3001
echo BITOK: http://localhost:8000
echo RabbitMQ: http://localhost:15672 (guest/guest)
echo DB: localhost:5432
echo wallet DB: cryptowallet_devphase3
echo BITOK DB: bitok_dev
echo user: postgres pass: postgres
echo ==========================================
echo.
timeout /t 3 /nobreak >nul
start http://localhost:3000
echo Press any key to close this launcher...
pause >nul
endlocal