feat: 管理员后台配置 + PostgreSQL 支持
管理员后台: - User 新增 IsAdmin,第一个注册的用户自动成为管理员; 老库升级时自动把最早注册的用户提升为管理员 - 新增 AdminMiddleware 与 /api/v1/admin/config 读写接口 - 可改开放注册、检查间隔、规则重试时长、日志保留条数,保存即生效 - 检查间隔改动后自动重排定时任务,为此把 cron 抽成 handler/scheduler - 监听地址、数据库、会话密钥等启动项不开放,接口只返回只读展示 - 设置页新增"系统配置"区块,仅管理员可见 PostgreSQL: - 新增 database_driver / database_dsn 配置,默认仍为 sqlite - postgres 用独立连接池(20/5/1h),sqlite 保持单连接避免写锁 - 启动时先 Ping,配置写错在启动阶段就报错而不是等到首次查询 - 支持 GOODBABY_DB_DRIVER / GOODBABY_DB_DSN 环境变量, 只给 DSN 时自动按 postgres 处理 文档: 新增 docs/database.md(含 compose 示例与切换说明),README 补充管理员与数据库章节 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -42,6 +42,8 @@ func HandleRegistry(c *gin.Context) {
|
||||
user := model.User{
|
||||
Username: registryRequest.Username,
|
||||
Password: registryRequest.Password,
|
||||
// 第一个注册的用户是管理员
|
||||
IsAdmin: userCount == 0,
|
||||
}
|
||||
|
||||
if err := user.IsValid(); err != nil {
|
||||
@@ -228,3 +230,19 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// AdminMiddleware 要求当前用户是管理员,需挂在 AuthMiddleware 之后
|
||||
func AdminMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
userInfo, err := GetUserInfoByGinCtx(c)
|
||||
if err != nil {
|
||||
response.AbortWith(c, http.StatusUnauthorized, "未登录")
|
||||
return
|
||||
}
|
||||
if !userInfo.IsAdmin {
|
||||
response.AbortWith(c, http.StatusForbidden, "需要管理员权限")
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user