Documentation
¶
Overview ¶
Package handlers 统一业务层:所有 Handler 严格遵循模板
func(ctx context.Context, query Q, body B) (resp R, err error)
本包不依赖任何 Web 框架,只依赖 context 与强类型请求/响应结构体, 由 internal/server(运行时)与 internal/openapi(文档生成)共同消费。
参数标签约定(两个适配器共用):
- Q:query 参数用 `query:"name"` 标签,路径参数用 `path:"name"` 标签
- B:POST/PUT/PATCH 传业务 body 结构体;无 body 的请求传 `any`
校验(internal/server 自动执行):
- 字段标签 `binding:"required"`:必填校验
- 结构体实现 `Validate() error` 方法:自定义校验(见 user.go CreateUserReq)
- 注册自定义校验器:server.AddValidator(...)
Index ¶
- Variables
- func CurrentUser(ctx context.Context) (any, error)
- func Health(ctx context.Context, _ NoReq, _ any) (map[string]string, error)
- func ListUsers(ctx context.Context, req ListUsersReq, _ any) (response.Paged[User], error)
- func WithUser(ctx context.Context, user any) context.Context
- type ChangePasswordReq
- type CreateUserReq
- type DeleteUserReq
- type DownloadSampleReq
- type Empty
- type FileStream
- type GetUserReq
- type ListUsersReq
- type MaskedUser
- type NoReq
- type User
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrNoUser = errors.New("current user not found") )
Functions ¶
Types ¶
type ChangePasswordReq ¶
type ChangePasswordReq struct {
ID string `path:"id" description:"用户ID"`
Password string `query:"password" validate:"required,min=8" description:"新密码"`
}
ChangePasswordReq 修改密码
func (*ChangePasswordReq) InTransform ¶
func (r *ChangePasswordReq) InTransform(ctx context.Context) error
InTransform 绑定后自动执行:去除首尾空白
type CreateUserReq ¶
type CreateUserReq struct {
// 姓名(注释)
Name string `json:"name" binding:"required"`
// 邮箱(注释)
Email string `json:"email" binding:"required" description:"邮箱"`
}
CreateUserReq 创建用户(演示:标签必填 + Validate() 自定义校验)
func (*CreateUserReq) InTransform ¶
func (r *CreateUserReq) InTransform(ctx context.Context) error
InTransform 入参规范化(升级能力):适配器在绑定后、校验前自动调用; trim 后 " Cara " 也能通过 required 必填检查
func (CreateUserReq) Validate ¶
func (r CreateUserReq) Validate() error
Validate 自定义校验:运行时适配器在校验阶段自动调用
type DeleteUserReq ¶
type DeleteUserReq struct {
ID string `path:"id" description:"用户ID"`
}
DeleteUserReq 删除用户
type DownloadSampleReq ¶
type DownloadSampleReq struct {
Name string `path:"name" description:"文件名(示例:sample.txt)"`
}
DownloadSampleReq 下载示例文件(演示 FileStream 二进制流,数据源为 go:embed 内存数据)
type Empty ¶
框架核心类型别名:业务层直接使用短名,类型本身来自 internal/contract
func DeleteUser ¶
DeleteUser 删除用户(Empty 响应:data 为 null)
type FileStream ¶
type FileStream = contract.FileStream
框架核心类型别名:业务层直接使用短名,类型本身来自 internal/contract
func DownloadSample ¶
func DownloadSample(ctx context.Context, req DownloadSampleReq, _ any) (*FileStream, error)
DownloadSample 下载示例文件
type GetUserReq ¶
type GetUserReq struct {
ID string `path:"id" description:"用户ID"`
}
GetUserReq 用户详情
type ListUsersReq ¶
type ListUsersReq struct {
Page int `query:"page" default:"1" description:"页码"`
Size int `query:"size" default:"10" description:"每页条数"`
}
ListUsersReq 用户列表(分页)
type MaskedUser ¶
MaskedUser 脱敏后的用户信息
func ChangePassword ¶
func ChangePassword(ctx context.Context, req ChangePasswordReq, _ any) (MaskedUser, error)
ChangePassword 修改密码(业务层零框架依赖:真实场景在此更新存储中的凭证哈希)
func (*MaskedUser) OutTransform ¶
func (u *MaskedUser) OutTransform(ctx context.Context) error
OutTransform 序列化前自动执行:邮箱脱敏 alice@example.com -> a***@example.com