# 小虾 SaaS 基础设施安装指南

**目标**：统一安装路径，便于管理  
**安装位置**：`F:\xiaoxia-infrastructure\`

---

## 一、安装 PostgreSQL

### 1.1 下载

**官方下载地址**：https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

**推荐版本**：PostgreSQL 16.x for Windows x86-64

**下载链接**：https://sbp.enterprisedb.com/getfile.jsp?fileid=1258893

---

### 1.2 安装

1. 运行下载的安装程序
2. **安装路径**：`F:\xiaoxia-infrastructure\PostgreSQL\16`
3. **端口**：5432（默认）
4. **超级用户密码**：设置一个你记得住的密码（如：`xiaoxia2026`）
5. 勾选 "Launch Stack Builder at exit"（可以取消）

---

### 1.3 配置

**创建数据库和用户**：

```sql
-- 使用 psql 或 pgAdmin 执行

-- 创建用户
CREATE USER xiaoxia WITH PASSWORD 'xiaoxia2026';

-- 创建数据库
CREATE DATABASE xiaoxia_saas OWNER xiaoxia;

-- 授权
GRANT ALL PRIVILEGES ON DATABASE xiaoxia_saas TO xiaoxia;
```

---

### 1.4 验证

```bash
# 设置环境变量（临时）
$env:PGPASSWORD="xiaoxia2026"

# 测试连接
psql -h localhost -U xiaoxia -d xiaoxia_saas -c "SELECT 1;"
```

**预期输出**：
```
 ?column? 
----------
        1
(1 row)
```

---

## 二、安装 Redis

### 2.1 下载

**Windows 版本**：https://github.com/tporadowski/redis/releases

**推荐版本**：Redis-x64-5.0.14.1.zip

**直接下载**：https://github.com/tporadowski/redis/releases/download/v5.0.14.1/Redis-x64-5.0.14.1.zip

---

### 2.2 安装

1. 解压到：`F:\xiaoxia-infrastructure\Redis\`
2. 目录结构：
   ```
   F:\xiaoxia-infrastructure\Redis\
   ├── redis-server.exe
   ├── redis-cli.exe
   └── redis.windows.conf
   ```

---

### 2.3 启动

**方式 1：临时启动（推荐用于测试）**

```bash
cd F:\xiaoxia-infrastructure\Redis
.\redis-server.exe
```

**方式 2：安装为 Windows 服务**

```bash
cd F:\xiaoxia-infrastructure\Redis
.\redis-server.exe --service-install redis.windows.conf --service-name RedisService
.\redis-server.exe --service-start
```

---

### 2.4 验证

```bash
cd F:\xiaoxia-infrastructure\Redis
.\redis-cli.exe ping
```

**预期输出**：`PONG`

---

## 三、安装 MinIO

### 3.1 下载

**官方下载**：https://dl.min.io/server/minio/release/windows-amd64/minio.exe

**直接下载**：
```bash
# 使用 PowerShell 下载
Invoke-WebRequest -Uri "https://dl.min.io/server/minio/release/windows-amd64/minio.exe" -OutFile "F:\xiaoxia-infrastructure\MinIO\minio.exe"
```

---

### 3.2 配置

创建数据目录：
```bash
mkdir F:\xiaoxia-infrastructure\MinIO\data
```

---

### 3.3 启动

```bash
cd F:\xiaoxia-infrastructure\MinIO

# 设置访问密钥
$env:MINIO_ROOT_USER="minioadmin"
$env:MINIO_ROOT_PASSWORD="minioadmin"

# 启动 MinIO
.\minio.exe server .\data --console-address ":9001"
```

**重要**：
- MinIO API 端口：`9000`
- MinIO 控制台端口：`9001`
- 访问控制台：http://localhost:9001

---

### 3.4 验证

**浏览器访问**：http://localhost:9001

**登录**：
- 用户名：`minioadmin`
- 密码：`minioadmin`

---

## 四、快速安装脚本

**创建一个一键安装脚本**（可选）

```powershell
# install-infrastructure.ps1

# 创建目录
New-Item -ItemType Directory -Force -Path "F:\xiaoxia-infrastructure\PostgreSQL"
New-Item -ItemType Directory -Force -Path "F:\xiaoxia-infrastructure\Redis"
New-Item -ItemType Directory -Force -Path "F:\xiaoxia-infrastructure\MinIO\data"

Write-Host "目录创建完成！"
Write-Host ""
Write-Host "接下来请手动完成："
Write-Host "1. 下载并安装 PostgreSQL 到 F:\xiaoxia-infrastructure\PostgreSQL\16"
Write-Host "2. 下载并解压 Redis 到 F:\xiaoxia-infrastructure\Redis\"
Write-Host "3. 下载 MinIO 到 F:\xiaoxia-infrastructure\MinIO\"
Write-Host ""
Write-Host "下载链接已保存在文档中。"
```

---

## 五、启动脚本

**创建统一启动脚本**

### start-infrastructure.ps1

```powershell
# 小虾 SaaS 基础设施启动脚本

Write-Host "=== 启动小虾 SaaS 基础设施 ===" -ForegroundColor Green

# 1. 检查并启动 PostgreSQL
Write-Host "`n[1/3] 检查 PostgreSQL..." -ForegroundColor Yellow
$pgService = Get-Service -Name "postgresql-x64-16" -ErrorAction SilentlyContinue
if ($pgService) {
    if ($pgService.Status -ne "Running") {
        Start-Service "postgresql-x64-16"
        Write-Host "PostgreSQL 已启动" -ForegroundColor Green
    } else {
        Write-Host "PostgreSQL 已在运行" -ForegroundColor Green
    }
} else {
    Write-Host "警告：PostgreSQL 服务未找到，请手动启动" -ForegroundColor Red
}

# 2. 启动 Redis
Write-Host "`n[2/3] 启动 Redis..." -ForegroundColor Yellow
$redisPath = "F:\xiaoxia-infrastructure\Redis\redis-server.exe"
if (Test-Path $redisPath) {
    Start-Process -FilePath $redisPath -WindowStyle Minimized
    Write-Host "Redis 已启动" -ForegroundColor Green
} else {
    Write-Host "警告：Redis 未找到，请检查路径" -ForegroundColor Red
}

# 3. 启动 MinIO
Write-Host "`n[3/3] 启动 MinIO..." -ForegroundColor Yellow
$minioPath = "F:\xiaoxia-infrastructure\MinIO\minio.exe"
if (Test-Path $minioPath) {
    $env:MINIO_ROOT_USER = "minioadmin"
    $env:MINIO_ROOT_PASSWORD = "minioadmin"
    Start-Process -FilePath $minioPath -ArgumentList "server","F:\xiaoxia-infrastructure\MinIO\data","--console-address",":9001" -WindowStyle Minimized
    Write-Host "MinIO 已启动" -ForegroundColor Green
    Write-Host "MinIO 控制台: http://localhost:9001" -ForegroundColor Cyan
} else {
    Write-Host "警告：MinIO 未找到，请检查路径" -ForegroundColor Red
}

Write-Host "`n=== 基础设施启动完成 ===" -ForegroundColor Green
Write-Host "`n服务地址："
Write-Host "  PostgreSQL: localhost:5432"
Write-Host "  Redis:      localhost:6379"
Write-Host "  MinIO API:  localhost:9000"
Write-Host "  MinIO UI:   http://localhost:9001"
```

---

## 六、停止脚本

### stop-infrastructure.ps1

```powershell
# 小虾 SaaS 基础设施停止脚本

Write-Host "=== 停止小虾 SaaS 基础设施 ===" -ForegroundColor Yellow

# 停止 Redis
Get-Process -Name "redis-server" -ErrorAction SilentlyContinue | Stop-Process -Force
Write-Host "Redis 已停止" -ForegroundColor Green

# 停止 MinIO
Get-Process -Name "minio" -ErrorAction SilentlyContinue | Stop-Process -Force
Write-Host "MinIO 已停止" -ForegroundColor Green

# PostgreSQL（通常作为服务运行，不停止）
Write-Host "PostgreSQL 保持运行（系统服务）" -ForegroundColor Cyan

Write-Host "`n=== 基础设施已停止 ===" -ForegroundColor Green
```

---

## 七、环境变量更新

安装完成后，更新 `.env.development`：

```env
# 数据库
DATABASE_URL=postgresql://xiaoxia:xiaoxia2026@localhost:5432/xiaoxia_saas
USE_IN_MEMORY_DB=false

# Redis
REDIS_URL=redis://localhost:6379/0
CELERY_BROKER_URL=redis://localhost:6379/1
CELERY_RESULT_BACKEND=redis://localhost:6379/2

# MinIO
OSS_ENDPOINT=localhost:9000
OSS_ACCESS_KEY_ID=minioadmin
OSS_ACCESS_KEY_SECRET=minioadmin
OSS_BUCKET_NAME=xiaoxia-saas
OSS_SECURE=false
OSS_PUBLIC_URL=http://localhost:9000

# JWT
JWT_SECRET_KEY=xiaoxia-dev-secret-key-please-change-in-production-min-32-chars
JWT_ALGORITHM=HS256
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=30
```

---

## 八、验证清单

安装完成后，执行验证：

```powershell
# PostgreSQL
psql -h localhost -U xiaoxia -d xiaoxia_saas -c "SELECT version();"

# Redis
cd F:\xiaoxia-infrastructure\Redis
.\redis-cli.exe ping

# MinIO
# 浏览器访问 http://localhost:9001
```

---

**准备好后，运行 `start-infrastructure.ps1` 启动所有服务！**
