Documentation
¶
Index ¶
- Constants
- Variables
- func Bind(r *http.Request, v any) error
- func Chain(h http.Handler, mws ...Middleware) http.Handler
- func Error(w http.ResponseWriter, r *http.Request, err error)
- func GetIdentity(ctx context.Context) any
- func NewHandler[Req any, Res any](fn HandlerFunc[Req, Res], opts ...Option) http.HandlerFunc
- func Validate(ctx context.Context, v any) error
- type AuthValidator
- type BasicValidator
- type CORSOptions
- type ErrorCoder
- type FileResponse
- type HandlerFunc
- type HttpError
- type Limiter
- type Middleware
- func AuthBasic(validator BasicValidator) Middleware
- func AuthBearer(validator AuthValidator) Middleware
- func CORS(opts CORSOptions) Middleware
- func DefaultCORS() Middleware
- func Logger(logFunc func(duration time.Duration, status int, path string)) Middleware
- func RateLimit(limiter Limiter) Middleware
- func Recovery(panicHook func(ctx context.Context, err interface{})) Middleware
- type Option
- type Response
- type SelfValidatable
- type Streamable
Constants ¶
const IdentityKey contextKey = "identity"
Variables ¶
var ( ErrBadRequest = &HttpError{400, "Bad Request"} ErrValidation = &HttpError{400, "Validation Failed"} ErrInternal = &HttpError{500, "Internal Server Error"} )
预定义错误
var Decoder = schema.NewDecoder()
var ErrorHook func(ctx context.Context, err error) = nil
ErrorHook 是一个回调函数,用于处理错误的副作用(如记录日志)。
var GetTraceID func(ctx context.Context) string = nil
GetTraceID 是一个钩子变量,允许外部注入获取 TraceID 的逻辑
var Validator = validator.New()
Functions ¶
func NewHandler ¶
func NewHandler[Req any, Res any](fn HandlerFunc[Req, Res], opts ...Option) http.HandlerFunc
NewHandler 将强类型的业务函数转换为标准的 http.HandlerFunc。
Types ¶
type AuthValidator ¶
AuthValidator 定义验证回调函数签名。 返回的 any 将被注入到 Context 中(例如 User 对象)。
type BasicValidator ¶
BasicValidator 定义 Basic Auth 验证回调。
type CORSOptions ¶
type CORSOptions struct {
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
ExposedHeaders []string
AllowCredentials bool
MaxAge int
}
CORSOptions 定义 CORS 配置
type ErrorCoder ¶
ErrorCoder 是一个接口,允许 error 自定义 HTTP 状态码。
type FileResponse ¶
FileResponse 是一个实现了 Streamable 的文件响应辅助类。
func (*FileResponse) Headers ¶
func (f *FileResponse) Headers() map[string]string
type HandlerFunc ¶
HandlerFunc 是业务逻辑的通用签名。 Req: 请求体结构体 (会自动 decode & validate) Res: 响应体结构体 (会自动 encode)
type Middleware ¶
Middleware 标准中间件定义
func AuthBearer ¶
func AuthBearer(validator AuthValidator) Middleware
AuthBearer Bearer Token 认证中间件。
func Logger ¶
func Logger(logFunc func(duration time.Duration, status int, path string)) Middleware
Logger 使用 httpsnoop 包装 ResponseWriter,确保兼容 Flusher/Hijacker
func Recovery ¶
func Recovery(panicHook func(ctx context.Context, err interface{})) Middleware
Recovery 捕获 Panic 防止服务崩溃
type Option ¶
type Option func(*config)
func NoEnvelope ¶
func NoEnvelope() Option
NoEnvelope 指示 Handler 不要使用标准的 {code, msg, data} 封口, 而是直接将 Res 序列化为 JSON。适用于 OAuth 等标准协议。
type Response ¶
type Response[T any] struct { Code int `json:"code"` Message string `json:"message"` Data T `json:"data,omitempty"` TraceID string `json:"trace_id,omitempty"` }
Response 是默认的统一响应信封。
type SelfValidatable ¶
SelfValidatable 是高性能验证接口。 如果 Request 结构体实现了此接口,将跳过反射验证。