管理员后台: - 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>
44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package model
|
|
|
|
import "fmt"
|
|
|
|
type UserRegistryReuest struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
// ChangePasswordRequest 修改密码请求
|
|
type ChangePasswordRequest struct {
|
|
OldPassword string `json:"old_password"`
|
|
NewPassword string `json:"new_password"`
|
|
}
|
|
|
|
// NotifyConfigRequest 修改提醒渠道配置的请求
|
|
type NotifyConfigRequest struct {
|
|
DingTalkConfig *string `json:"dingtalk_config"`
|
|
}
|
|
|
|
type User struct {
|
|
ID uint `json:"id" gorm:"primaryKey"`
|
|
CreateAt int64 `json:"create_at"`
|
|
Username string `gorm:"uniqueIndex" json:"username"`
|
|
Password string `json:"-"`
|
|
// 管理员可以修改系统配置。第一个注册的用户自动成为管理员。
|
|
IsAdmin bool `gorm:"default:false" json:"is_admin"`
|
|
// 钉钉机器人配置(JSON 字符串),用于接收提醒
|
|
DingTalkConfig *string `json:"dingtalk_config"`
|
|
}
|
|
|
|
func (u *User) IsValid() error {
|
|
if u.Username == "" || u.Password == "" {
|
|
return fmt.Errorf("用户名或密码不能为空")
|
|
}
|
|
if len(u.Username) > 32 || len(u.Username) < 2 {
|
|
return fmt.Errorf("用户名过长或过短")
|
|
}
|
|
if len(u.Password) > 64 || len(u.Password) < 6 {
|
|
return fmt.Errorf("密码过长或过短")
|
|
}
|
|
return nil
|
|
}
|