middleware

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package middleware provides reusable HTTP middleware. Package middleware 提供可复用 HTTP 中间件。

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 requests with method, path, status, and latency. Call r.Use(AccessLog(...)). AccessLog 记录请求的 method、path、status 和 latency。 调用 r.Use(AccessLog(...))。

Example / 示例:

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

func AllowedMethodsForRoute

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

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

func LimitBody

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

LimitBody sets http.MaxBytesReader when maxBytes > 0. Call LimitBody before decoding the request body. LimitBody 在 maxBytes > 0 时设置 http.MaxBytesReader。 在解码请求体前调用 LimitBody。

func MethodNotAllowedResponder

func MethodNotAllowedResponder(routes chi.Routes) http.HandlerFunc

MethodNotAllowedResponder returns a 405 handler that sets the Allow header. Call r.MethodNotAllowed(MethodNotAllowedResponder(r)). MethodNotAllowedResponder 返回会设置 Allow 头的 405 handler。 调用 r.MethodNotAllowed(MethodNotAllowedResponder(r))。 Example / 示例:

r.MethodNotAllowed(middleware.MethodNotAllowedResponder(r))

func NotFoundHandler

func NotFoundHandler(staticHandler http.Handler) http.HandlerFunc

NotFoundHandler returns JSON 404 for /api paths and delegates the rest to staticHandler. Call r.NotFound(NotFoundHandler(staticHandler)). NotFoundHandler 对 /api 路径返回 JSON 404,其余交给 staticHandler。 调用 r.NotFound(NotFoundHandler(staticHandler))。 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. Call r.Use(RateLimit(opts)). RateLimit 返回限流中间件。 调用 r.Use(RateLimit(opts))。 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 from the client IP prefix. Forwarded headers are used only when TrustedProxies is set. RateLimitKeyByIP 根据客户端 IP 前缀构造限流键。 只有设置 TrustedProxies 时才会使用转发头。

func RequireJSONBody added in v0.1.7

func RequireJSONBody(next http.Handler) http.Handler

RequireJSONBody requires Content-Type: application/json for requests with a body. Use routes.Use(RequireJSONBody) on routes that decode JSON. RequireJSONBody 要求带 body 的请求使用 Content-Type: application/json。 对会解码 JSON 的路由使用 routes.Use(RequireJSONBody)。 Example / 示例:

r.Post("/login", "Login", routes.Func(loginHandler), routes.Use(middleware.RequireJSONBody))

Types

type AccessLogOptions

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

AccessLogOptions configures AccessLog. AccessLogOptions 配置 AccessLog。

type RateLimitKeyOptions

type RateLimitKeyOptions struct {
	TrustedProxies []netip.Prefix
}

RateLimitKeyOptions configures RateLimitKeyByIP. RateLimitKeyOptions 配置 RateLimitKeyByIP。

type RateLimitOptions

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

RateLimitOptions configures RateLimit. RateLimitOptions 配置 RateLimit。

Jump to

Keyboard shortcuts

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