# 🔌 AGE OS API 代理层 — Nginx 配置参考
#
# 将此配置添加到 guanghulab.com 的 Nginx server block 中，
# 使前端可以通过 /api/* 路径访问后端代理。
#
# 前端（GitHub Pages）→ Nginx → API 代理（Node.js :3721）→ 模型 API

# Persona Studio API → persona-studio 后端端口 3002（含 CORS 跨域支持）
location /api/ps/ {
    add_header Access-Control-Allow-Origin * always;
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
    add_header Access-Control-Allow-Headers "Content-Type, Authorization" always;
    if ($request_method = OPTIONS) {
        return 204;
    }
    proxy_pass http://127.0.0.1:3002/api/ps/;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_read_timeout 90s;
}

# Webhook 路由 → 后端服务端口 3000（飞书事件回调、广播推送等）
location /webhook/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Content-Type $content_type;
}

# API 代理转发（通用 AI 聊天）
location /api/ {
    proxy_pass http://127.0.0.1:3721;
    proxy_http_version 1.1;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    # SSE 流式响应必需
    proxy_set_header Connection '';
    proxy_buffering off;
    proxy_cache off;
    chunked_transfer_encoding on;

    # 长连接超时（与 Node.js api-proxy 的 60s 连接超时对齐，流式响应可更长）
    proxy_read_timeout 90s;
    proxy_send_timeout 60s;
}

# Persona Studio WebSocket → 预览进度推送端口 3002
location /ws/preview {
    proxy_pass http://127.0.0.1:3002;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 86400;
}

# ═══ AI写网文平台 · 端口 3100 ═══

# AI写网文平台前端
location /writing/ {
    alias /var/www/guanghulab/writing-platform/frontend/dist/;
    try_files $uri $uri/ /writing/index.html;
}

# AI写网文平台后端 API
location /api/writing/ {
    proxy_pass http://127.0.0.1:3100/api/writing/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_read_timeout 90s;
}

# AI伙伴 WebSocket 对话
location /writing-ai/ {
    proxy_pass http://127.0.0.1:3100/writing-ai/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 86400;
}
