middleware

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package middleware provides reusable HTTP middleware. 可复用的 HTTP 中间件:JSONOnly、NotFound、MethodNotAllowed、RateLimit、AccessLog。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLog

func AccessLog(opts AccessLogOptions) func(http.Handler) http.Handler

AccessLog logs HTTP requests with structured fields. AccessLog 以结构化字段记录 HTTP 请求。

Example / 示例:

r.Use(middleware.AccessLog(middleware.AccessLogOptions{Logger: logger}))

func AllowedMethodsForRoute

func AllowedMethodsForRoute(routes chi.Routes, r *http.Request) []string

AllowedMethodsForRoute returns HTTP methods allowed for the matched route. AllowedMethodsForRoute 返回匹配路由允许的 HTTP 方法。

func JSONOnly

func JSONOnly(next http.HandlerFunc) http.HandlerFunc

JSONOnly enforces Content-Type: application/json for requests with body. JSONOnly 强制带 body 的请求使用 application/json。 Example / 示例:

r.Post("/login", middleware.JSONOnly(handleLogin))

func LimitBody

func LimitBody(maxBytes int64) func(http.Handler) http.Handler

LimitBody sets MaxBytesReader on the request body when maxBytes > 0. LimitBody 在 maxBytes > 0 时为请求体设置 MaxBytesReader。 It does not change error handling; callers own the response semantics. 它不会改变错误处理逻辑;响应语义由调用方决定。

func MethodNotAllowedResponder

func MethodNotAllowedResponder(routes chi.Routes) http.HandlerFunc

MethodNotAllowedResponder returns a 405 handler with proper Allow header. MethodNotAllowedResponder 返回带正确 Allow 头的 405 处理器。 Example / 示例:

r.MethodNotAllowed(middleware.MethodNotAllowedResponder(r))

func NotFoundHandler

func NotFoundHandler(staticHandler http.Handler) http.HandlerFunc

NotFoundHandler returns JSON 404 for /api paths, otherwise falls back to static. NotFoundHandler 对 /api 路径返回 JSON 404,其它交给静态资源。 Example / 示例:

staticHandler := http.FileServer(http.Dir("web/dist"))
r.NotFound(middleware.NotFoundHandler(staticHandler))

func RateLimit

func RateLimit(opts RateLimitOptions) func(http.Handler) http.Handler

RateLimit returns a rate-limit middleware. RateLimit 返回限流中间件。 Example / 示例:

r.Use(middleware.RateLimit(middleware.RateLimitOptions{Requests: 100, Window: time.Minute}))

func RateLimitKeyByIP

func RateLimitKeyByIP(ipv4PrefixBits, ipv6PrefixBits int, opts RateLimitKeyOptions) httprate.KeyFunc

RateLimitKeyByIP builds a rate-limit key using IP prefix. RateLimitKeyByIP 使用 IP 前缀构建限流键。 By default it uses RemoteAddr only; forwarded headers are used only when TrustedProxies is set. 默认仅使用 RemoteAddr;只有显式配置 TrustedProxies 时才会使用转发头。

Types

type AccessLogOptions

type AccessLogOptions struct {
	Disabled bool
	Logger   logging.Logger
	MinLevel logging.Level
	Skip     func(r *http.Request, status int) bool
	ClientIP clientip.Options
}

AccessLogOptions configures access logging. AccessLogOptions 配置访问日志。

type RateLimitKeyOptions

type RateLimitKeyOptions struct {
	TrustedProxies []netip.Prefix
}

RateLimitKeyOptions configures how client IPs are derived for rate-limit keys. RateLimitKeyOptions 配置限流键如何提取客户端 IP。

type RateLimitOptions

type RateLimitOptions struct {
	Requests int
	Window   time.Duration
	KeyFunc  httprate.KeyFunc
	OnLimit  func(http.ResponseWriter, *http.Request)
}

RateLimitOptions configures rate limiting. RateLimitOptions 配置限流。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL