--- title: OpenCode AI Integration with Nginx Security emoji: 🤖 colorFrom: green colorTo: blue sdk: docker pinned: false --- # 🤖 OpenCode AI + Nginx Security Integration 这个 Hugging Face Space 集成了 [OpenCode](https://opencode.ai) AI 编程代理与安全的企业级 Nginx 服务器,提供完整的 AI 开发平台。 ## 🎯 功能特性 ### 🔐 安全防护 - **HTTP Basic Authentication** (用户名: `admin`, 密码: `admin123`) - **Lua 脚本安全过滤** - 防止恶意请求 - **API 访问控制** - 仅允许授权访问 - **请求日志记录** - 完整的访问审计 ### 🤖 AI 编程能力 - **代码生成和修改** - 基于 LLM 的智能编程 - **项目分析** - 自动理解代码库结构 - **多模型支持** - 支持各种 LLM 提供商 - **实时对话** - 流式 AI 交互 ### 🛠️ 企业级特性 - **高可用性** - Nginx 负载均衡 - **高性能** - 连接池和缓存优化 - **可扩展** - 模块化架构 - **监控就绪** - 健康检查和指标 ## 🚀 快速开始 ### 📡 API 访问方式 #### 1. 主页访问 ```bash curl -u admin:admin123 https://airsltd-ocngx.hf.space/ ``` #### 2. 健康检查 ```bash curl -u admin:admin123 https://airsltd-ocngx.hf.space/health ``` #### 3. OpenCode API (通过代理) ```bash # API 文档 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/doc # OpenCode 健康检查 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/global/health # 创建 AI 编程会话 curl -u admin:admin123 -X POST \ -H "Content-Type: application/json" \ -d '{"title": "AI Coding Session"}' \ https://airsltd-ocngx.hf.space/opencode/session # 发送 AI 请求 curl -u admin:admin123 -X POST \ -H "Content-Type: application/json" \ -d '{ "parts": [{"type": "text", "text": "创建一个 Python Hello World 应用"}] }' \ https://airsltd-ocngx.hf.space/opencode/session/{session_id}/message ``` ## 📋 API 端点 ### 🔐 认证保护的端点 | 端点 | 方法 | 描述 | |------|------|------| | `/` | GET | 主页和介绍 | | `/health` | GET | 服务健康检查 | | `/opencode/doc` | GET | OpenCode API 文档 (Swagger) | | `/opencode/*` | ALL | OpenCode API 代理 | ### 🤖 OpenCode API #### 核心 API ```bash # 健康检查 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/global/health # API 文档 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/doc # 项目管理 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/project # 会话管理 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/session # 提供商管理 curl -u admin:admin123 https://airsltd-ocngx.hf.space/opencode/provider ``` #### Nginx 原生端点 ```bash # 主页 curl -u admin:admin123 https://airsltd-ocngx.hf.space/ # 健康检查 curl -u admin:admin123 https://airsltd-ocngx.hf.space/health ``` ## 🏗️ 架构设计 ``` 用户请求 → Nginx (7860) → OpenCode (57860) → AI 模型 ↓ ↓ ↓ Basic Auth → 代理转发 → AI 处理 Lua 过滤 → CORS 控制 → 代码生成 ``` ### 🔧 组件说明 1. **Nginx 服务器** - 监听端口:7860 - 认证:HTTP Basic Auth - 安全:Lua 脚本过滤 - 代理:转发到 OpenCode 2. **OpenCode 服务器** - 监听端口:57860 - 功能:AI 编程代理 - API:OpenAPI 3.1 规范 - 模型:支持多种 LLM ## 🛡️ 安全特性 ### 认证保护 - ✅ HTTP Basic Auth (admin/admin123) - ✅ 用户名日志掩码 - ✅ 会话管理 - ✅ 自动清理 ### 请求过滤 - ✅ Lua 脚本过滤恶意 User-Agent - ✅ CORS 跨域控制 - ✅ 请求速率限制 - ✅ 安全头设置 ### 访问控制 - ✅ 仅允许授权 API 调用 - ✅ 请求路径验证 - ✅ 错误处理和日志 - ✅ 健康检查监控 ## 🔧 配置说明 ### 认证信息 ``` 用户名: admin 密码: admin123 ``` ### 环境变量 ```bash GATEWAY_HOST=127.0.0.1 GATEWAY_PORT=57860 ``` ### Docker 配置 - **Nginx 端口**: 7860 - **OpenCode 端口**: 57860 - **Node.js 环境**: 内置安装 - **自动启动**: 脚本化管理 ## 📊 监控和健康检查 ### 健康检查响应 ```json { "healthy": true, "version": "1.27.1.2", "service": "OpenResty + OpenCode Integration" } ``` ### 认证头部 ```http X-Powered-By: OpenResty X-Auth-Type: Basic + Lua Server: openresty/1.27.1.2 ``` ## 🧪 使用示例 ### 1. 基础 AI 对话 ```python import requests # 创建会话 session = requests.post( "https://airsltd-ocngx.hf.space/opencode/session", auth=("admin", "admin123"), json={"title": "Python Development"} ) session_data = session.json() # 发送请求 response = requests.post( f"https://airsltd-ocngx.hf.space/opencode/session/{session_data['id']}/message", auth=("admin", "admin123"), json={ "parts": [{"type": "text", "text": "创建一个 Flask Web 应用"}] } ) ``` ### 2. 代码分析 ```python # 搜索文件 response = requests.get( "https://airsltd-ocngx.hf.space/opencode/find/file", auth=("admin", "admin123"), params={"query": "main.py"} ) # 读取文件内容 response = requests.get( "https://airsltd-ocngx.hf.space/opencode/file/content", auth=("admin", "admin123"), params={"path": "/path/to/file.py"} ) ``` ### 3. 项目管理 ```python # 获取项目信息 response = requests.get( "https://airsltd-ocngx.hf.space/opencode/project/current", auth=("admin", "admin123") ) ``` ## 🔧 开发和调试 ### 检查服务状态 ```bash # 检查 OpenCode 状态 curl -s http://127.0.0.1:57860/global/health # 检查 Nginx 状态 curl -u admin:admin123 http://localhost:7860/health # 查看 Nginx 日志 docker logs [container_name] | grep nginx # 查看 OpenCode 日志 docker logs [container_name] | grep opencode ``` ### 故障排除 1. **认证失败** - 检查用户名密码 (admin/admin123) 2. **OpenCode 不可达** - 确认内部端口 57860 3. **API 代理失败** - 检查 nginx 配置中的代理设置 4. **CORS 错误** - 确认正确的 Origin 头部设置 ## 📈 性能特性 - 🚀 **连接池** - Nginx 高性能连接复用 - ⚡ **缓存优化** - 静态资源缓存 - 🔄 **负载均衡** - 支持水平扩展 - 📊 **监控指标** - 实时性能数据 - 🛡️ **安全加速** - Lua 脚本高效执行 ## 🎯 应用场景 ### 1. AI 辅助开发 - 自动生成业务代码 - 重构和优化现有代码 - Bug 修复和调试 - 代码审查和建议 ### 2. 自动化开发 - CI/CD 集成 - 批量代码生成 - 测试用例生成 - 文档自动生成 ### 3. 代码库分析 - 大型项目理解 - 依赖关系分析 - 架构图生成 - 最佳实践建议 --- ## 🎉 开始使用 这个集成为您提供了一个完整的 AI 开发平台: 1. 🔐 **安全访问** - 企业级安全保护 2. 🤖 **AI 能力** - 强大的编程助手 3. 🛠️ **可靠性能** - 高性能 Nginx 代理 4. 📊 **完整监控** - 健康检查和日志 5. 🔧 **易于集成** - 标准 REST API 立即开始您的 AI 开发之旅! 📖 详细文档:[OpenCode 官方文档](https://opencode.ai/docs) 🚀 项目地址:[GitHub 仓库](https://github.com/anomalyco/opencode) 💬 社区支持:[Discord 频道](https://opencode.ai/discord)