Version: 2.0
Status: Active
Last Updated: 2026-01-09
本文档仅记录项目特有的 API 设计决策。通用 REST 规范参考 RFC 9110
决策: 目前不使用版本化(快速 MVP)
✅ GET /api/users
❌ GET /api/v1/users
限制: 最多 3 层嵌套
✅ /api/projects/123/files
❌ /api/projects/123/models/456/fields/789/validations
| 场景 | 方式 | Header |
|---|---|---|
| Web 浏览器 | Session Cookie | Cookie: ory_kratos_session=xxx |
| API 客户端 | JWT | Authorization: Bearer xxx |
| 服务调用 | API Key | X-API-Key: sk_live_xxx |
API Key 格式: {prefix}_{env}_{random} (例: sk_live_abc123)
安全规则: 仅 Header 传递,禁止 URL 参数
详见: 认证模块规范
// 成功
{"success": true, "code": 200, "message": "OK", "data": {...}}
// 错误
{"success": false, "code": 400, "message": "参数错误", "error": {"code": "VAL_INVALID_PARAM_001", "message": "..."}}实现: pkg/response
{
"data": {
"items": [...],
"pagination": {"total": 100, "page": 1, "pageSize": 20, "totalPages": 5}
}
}格式: <MODULE>_<TYPE>_<NUMBER>
示例:
AUTH_INVALID_TOKEN_001RES_NOT_FOUND_001VAL_MISSING_FIELD_002SYS_DATABASE_ERROR_003
HTTP 状态码映射:
- 400 →
VAL_*(参数错误) - 401 →
AUTH_*(未认证) - 403 →
PERM_*(无权限) - 404 →
RES_NOT_FOUND_* - 429 →
RATE_LIMIT_* - 500 →
SYS_*(系统错误)
定义位置: internal/errors/codes.go
| 层级 | 格式 | 示例 |
|---|---|---|
| JSON 输出 | snake_case |
user_id, created_at |
| Go 代码 | CamelCase |
UserID, CreatedAt |
| 枚举值 | lowercase |
active, pending |
| 布尔字段 | is_* / has_* |
is_active |
| URL | 复数名词 | /users, /projects |
- 免费用户: 1000/小时
- 付费用户: 10000/小时
- 管理员: 无限制
白名单管理 (internal/middleware/cors.go),开发环境允许 localhost:3000
所有公开 API 必须添加注解:
// @Summary Get configuration item
// @Tags config
// @Param key query string true "Configuration key"
// @Success 200 {object} GetConfigResponse
// @Failure 400 {object} ErrorResponse
// @Router /config [get]
func (h *Handler) GetConfig(w http.ResponseWriter, r *http.Request) { ... }- 添加注解 → 2.
make swagger→ 3. 验证/api/docs/→ 4. 提交core/docs/
详见: Story 11
- 分页:
?page=1&pageSize=20 - 过滤:
?status=active&owner_id=123 - 排序:
?sort=created_at&order=desc - 搜索:
?q=keyword
维护者: Architect Agent