Files
cloudnest/start-local.ps1

115 lines
5.8 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# CloudNest 本地开发启动脚本 (Windows PowerShell)
# CloudNest Local Development Startup Script (Windows PowerShell)
#
# 前置条件 / Prerequisites:
# 1. 安装 Go 1.25+ (https://go.dev/dl/)
# 2. 安装 Docker Desktop (https://www.docker.com/products/docker-desktop/)
#
# 用法 / Usage:
# .\start-local.ps1 # 使用默认 dev 配置启动(自动启动基础设施)
# $env:CLOUDNEST_ENV="test"; .\start-local.ps1 # 使用 test 配置启动
#
# 环境变量 / Environment Variables:
# CLOUDNEST_ENV - 运行环境: dev / test / prod(默认: dev
# SKIP_INFRA - 是否跳过基础设施启动: true / false(默认: false
# DB_PASSWORD - MySQL 密码(默认: sun900120
# DB_NAME - 数据库名(默认: cloudnest
# REDIS_PORT - Redis 端口(默认: 6379
# MINIO_ACCESS_KEY - MinIO 访问密钥(默认: minioadmin
# MINIO_SECRET_KEY - MinIO 秘密密钥(默认: minioadmin
$ErrorActionPreference = "Stop"
function Test-Command($cmd) {
return [bool](Get-Command $cmd -ErrorAction SilentlyContinue)
}
# 设置默认环境 / Set default environment
if (-not $env:CLOUDNEST_ENV) {
$env:CLOUDNEST_ENV = "dev"
}
Write-Host "=== CloudNest 本地开发启动 ===" -ForegroundColor Cyan
Write-Host "环境 / Environment: $($env:CLOUDNEST_ENV)" -ForegroundColor Cyan
# 检查依赖 / Check dependencies
if (-not (Test-Command "go")) {
Write-Host "[错误] 未找到 Go。请先安装 Go 1.25+: https://go.dev/dl/" -ForegroundColor Red
exit 1
}
# 检查基础设施 / Check infrastructure
$skipInfra = $false
if ($env:SKIP_INFRA -eq "true") {
$skipInfra = $true
Write-Host "[信息] SKIP_INFRA=true,跳过基础设施启动" -ForegroundColor Yellow
} else {
if (-not (Test-Command "docker")) {
Write-Host "[错误] 未找到 Docker。请先安装 Docker Desktop: https://www.docker.com/products/docker-desktop/" -ForegroundColor Red
Write-Host " 或者设置 SKIP_INFRA=true 使用外部基础设施" -ForegroundColor Yellow
exit 1
}
# 检查基础设施是否已启动 / Check if infrastructure is running
$mysqlRunning = docker ps --filter "name=cloudnest-mysql" --format "{{.Names}}" 2>$null
$redisRunning = docker ps --filter "name=cloudnest-redis" --format "{{.Names}}" 2>$null
$minioRunning = docker ps --filter "name=cloudnest-minio" --format "{{.Names}}" 2>$null
if (-not $mysqlRunning -or -not $redisRunning -or -not $minioRunning) {
Write-Host "[信息] 基础设施未运行,尝试自动启动..." -ForegroundColor Yellow
# 构建 Docker Compose 命令(支持环境变量覆盖)
$composeCmd = "docker compose -f docker-compose.infra.yml up -d"
Write-Host "[命令] $composeCmd" -ForegroundColor Cyan
Invoke-Expression $composeCmd
Write-Host "[信息] 等待 MySQL 就绪..." -ForegroundColor Yellow
$maxWait = 60
$waited = 0
while ($waited -lt $maxWait) {
$healthy = docker inspect --format='{{.State.Health.Status}}' cloudnest-mysql 2>$null
if ($healthy -eq "healthy") { break }
Start-Sleep -Seconds 2
$waited += 2
Write-Host " 已等待 ${waited}s..." -ForegroundColor DarkGray
}
if ($healthy -ne "healthy") {
Write-Host "[警告] MySQL 健康检查超时,可能还在初始化,继续尝试启动服务..." -ForegroundColor Yellow
}
} else {
Write-Host "[信息] 基础设施已运行" -ForegroundColor Green
}
}
# 下载 Go 依赖 / Download Go dependencies
Write-Host "[信息] 下载 Go 依赖..." -ForegroundColor Cyan
$env:GOROOT = "D:\go"
$env:PATH = "D:\go\bin;" + $env:PATH
go mod tidy
if ($LASTEXITCODE -ne 0) {
Write-Host "[错误] go mod tidy 失败" -ForegroundColor Red
exit 1
}
# 编译并启动 Auth Service / Build and start Auth Service
Write-Host "[信息] 启动 Auth Service (端口: 8081)..." -ForegroundColor Cyan
Start-Process powershell -ArgumentList "-NoExit", "-Command", "`$Host.UI.RawUI.WindowTitle='Auth Service'; `$env:CLOUDNEST_ENV='$($env:CLOUDNEST_ENV)'; `$env:GOROOT='D:\go'; `$env:PATH='D:\go\bin;' + `$env:PATH; cd '$PWD'; go run ./cmd/auth-service" -WindowStyle Normal
# 编译并启动 File Service (固定端口 8082,避免与 Auth Service 冲突)
# Build and start File Service (fixed port 8082 to avoid conflict with Auth Service)
Write-Host "[信息] 启动 File Service (端口: 8082)..." -ForegroundColor Cyan
Start-Process powershell -ArgumentList "-NoExit", "-Command", "`$Host.UI.RawUI.WindowTitle='File Service'; `$env:CLOUDNEST_ENV='$($env:CLOUDNEST_ENV)'; `$env:SERVER_PORT='8082'; `$env:GOROOT='D:\go'; `$env:PATH='D:\go\bin;' + `$env:PATH; cd '$PWD'; go run ./cmd/file-service" -WindowStyle Normal
Write-Host ""
Write-Host "=== 本地服务已启动 ===" -ForegroundColor Green
Write-Host "配置文件 / Config: configs/config.$($env:CLOUDNEST_ENV).yaml" -ForegroundColor Green
Write-Host "Auth Service: http://localhost:8081 (若占用则自动切换端口)" -ForegroundColor Green
Write-Host "File Service: http://localhost:8082 (若占用则自动切换端口)" -ForegroundColor Green
Write-Host "MinIO Console: http://localhost:9001 (minioadmin / minioadmin)" -ForegroundColor Green
Write-Host ""
Write-Host "测试命令 / Test commands:" -ForegroundColor Cyan
Write-Host " 注册 / Register: curl -X POST http://localhost:8081/api/v1/auth/register -H 'Content-Type: application/json' -d '{\"username\":\"admin\",\"password\":\"123456\"}'"
Write-Host " 登录 / Login: curl -X POST http://localhost:8081/api/v1/auth/login -H 'Content-Type: application/json' -d '{\"username\":\"admin\",\"password\":\"123456\"}'"
Write-Host ""
Write-Host "按 Enter 退出此窗口(服务进程会继续在后台运行)" -ForegroundColor DarkGray
Read-Host