Skip to content

API 约定 ​

地址与响应信封 ​

  • 基础地址:http://localhost:8001/api/v1(app.api_prefix 可配)。
  • 统一信封(internal/pkg/response),与 Python 版逐字段一致:
go
type Body struct {
    Code int    `json:"code"`
    Msg  string `json:"msg"`
    Data any    `json:"data"`
}
json
// 成功
{ "code": 200, "msg": "success", "data": { ... } }
// 分页 data
{ "total": 123, "page": 1, "page_size": 20, "items": [ ... ] }
  • i18n 变体:response.SuccessT / ErrorT 按 Locale 中间件解析的 Accept-Language 翻译 msg(core.T(locale, key, params)),支持 {name} 占位符插值。
  • 字段命名全局 snake_case(struct tag 保证),三版底座一致。

错误码语义 ​

与 Python 版对齐:200 成功(前端判据 code === 200);401 未登录 / token 失效;403 权限不足;404 资源不存在;409 冲突;422 校验失败;429 限流(全局 API 限流 + 登录防爆破 + AI 配额)。

认证 ​

text
Authorization: Bearer <access_token>
  • 登录:POST /api/v1/auth/login(middleware.LoginGuard 前置 + RateLimit("login", 10/min) 防爆破)。
  • 刷新:POST /api/v1/auth/refresh —— 免登录(access 过期后才能换新,放 authed 组会死锁)。
  • 登出:POST /api/v1/auth/logout → jti/token 进内存 TokenStore 黑名单(后台 30 分钟批量清理;token_version 改密失效双保险)。

路由三级分组(router.go 实景) ​

组注册方式典型路由
publicapi.Group("")/auth/login、/auth/refresh、/settings/public、/health、/i18n/locales、/i18n/messages/:locale
authedUse(middleware.JWTAuth())/auth/userinfo、/auth/profile(GET/PUT)、/auth/profile/password、/auth/logout
permUse(JWTAuth) + 行内 RequirePermission/users(system:user:list)、/users/:id 等

行内权限写法:

go
users.GET("", middleware.RequirePermission("system:user:list"), userH.List)
users.POST("", middleware.RequirePermission("system:user:add"), userH.Create)
users.POST("/:id/kick", middleware.RequirePermission("system:user:edit"), userH.Kick)  // 在线踢下线

主要资源 ​

分组文件说明
认证auth.go登录 / 刷新 / 用户信息 / 个人资料
用户 / 角色 / 菜单 / 部门user.go 等RBAC(含在线列表 / 踢下线;/roles/all 免权限)
文件文件管理上传下载
日志操作 / 登录日志AuditQueue 异步入库
设置system.go / setting键值配置(admin_path 运行时覆盖)
插件plugin.go列表 / 启停 / ZIP 上传(L2)
AIai_provider.go ai_chat.go ai_chat_runs.go ai_chat_stream.go ai_tools.go ai_runner.go供应商密钥 / 会话 / Run / SSE 流式 / Function Calling
MCPmcp.go工具 / 资源 / 提示词 / 审计日志
仪表盘dashboard.go业务统计
i18ni18n.go语言注册表 + 翻译条目(DB 覆盖层)

新增一个业务模块的标准写法(以「公告管理」为例) ​

  1. 模型:internal/model/notice.go 嵌入 BaseModel(自动获得 id / created_at / updated_at / deleted 软删除),TableName() { return "sys_notice" };加入 bootstrap.allModels(开发期 auto_migrate)。
  2. schema:internal/schema/notice.go 请求 / 响应 DTO(json tag snake_case)。
  3. DAL:internal/dal/notice.go 数据访问。
  4. handler:internal/api/notice.go,返回 response.Success(...);行内挂 middleware.RequirePermission("system:notice:list")。
  5. 路由:router.go 的 perm 组注册(perm.Group("/notices"))。
  6. 菜单 / 权限码:种子 sys_menu(permission 存 system:notice:*);前端共用 Vue 源码按菜单 component 建页面。

语义对齐说明 ​

三版契约逐一对齐;遇到行为差异以 Python 版为准,并在 KevinLiss/ApeAdmin-Gin 仓库提 issue。

ApeHub 插件生态