Documentation
¶
Index ¶
- Constants
- Variables
- func BodyLimit(limit string) mux.MiddlewareFunc
- func CSRF() mux.MiddlewareFunc
- func CSRFWithConfig(config CSRFConfig) mux.MiddlewareFunc
- func ExtractorMiddleware(extractors ...ValuesExtractor) mux.MiddlewareFunc
- func GenerateUUID() string
- func GetError(r *http.Request) error
- func GetHandlerResponse(r *http.Request) interface{}
- func HandlerResponse(next http.Handler) http.Handler
- func JwtMiddleware(config auth.JwtConfig) mux.MiddlewareFunc
- func Recovery(next http.Handler) http.Handler
- func RequestID(logger *slog.Logger) mux.MiddlewareFunc
- func RequestLog(logger *slog.Logger) mux.MiddlewareFunc
- func Secure() mux.MiddlewareFunc
- func SecureWithConfig(config SecureConfig) mux.MiddlewareFunc
- func SetError(r *http.Request, err error)
- func SetHandlerResponse(r *http.Request, response interface{})
- type BodyLimitConfig
- type CSRFConfig
- type DefaultHandlerResponse
- type SecureConfig
- type ValuesExtractor
Constants ¶
const RequestIDKey = "requestID"
RequestIDKey is the key used to store the request ID in the context
Variables ¶
var DefaultCSRFConfig = CSRFConfig{ Skipper: func(r *http.Request) bool { return false }, TokenLength: 32, TokenLookup: "header:X-CSRF-Token", ContextKey: "csrf", CookieName: "_csrf", CookieMaxAge: 86400, CookieSameSite: http.SameSiteDefaultMode, }
DefaultCSRFConfig 是默认的 CSRF 中间件配置。
var DefaultSecureConfig = SecureConfig{ Skipper: func(r *http.Request) bool { return false }, XSSProtection: "1; mode=block", ContentTypeNosniff: "nosniff", XFrameOptions: "SAMEORIGIN", HSTSPreloadEnabled: false, }
DefaultSecureConfig 是 Secure 中间件的默认配置。
var ErrCSRFInvalid = errors.New("invalid csrf token")
ErrCSRFInvalid 在 CSRF 检查失败时返回
var ExtractedValuesKey = &contextKey{"extracted_values"}
ExtractedValuesKey is the key used to store extracted values in the context
Functions ¶
func CSRFWithConfig ¶
func CSRFWithConfig(config CSRFConfig) mux.MiddlewareFunc
CSRFWithConfig 返回一个带配置的 CSRF 中间件。
func ExtractorMiddleware ¶
func ExtractorMiddleware(extractors ...ValuesExtractor) mux.MiddlewareFunc
ExtractorMiddleware is a middleware that extracts values using the provided extractors
func GetHandlerResponse ¶
func HandlerResponse ¶
HandlerResponse is the middleware function for handling response and errors
func JwtMiddleware ¶
func JwtMiddleware(config auth.JwtConfig) mux.MiddlewareFunc
JwtMiddleware JWT 中间件
func RequestID ¶
func RequestID(logger *slog.Logger) mux.MiddlewareFunc
RequestID is a middleware that adds a unique request id to the request context
func RequestLog ¶
func RequestLog(logger *slog.Logger) mux.MiddlewareFunc
RequestLog is a middleware that logs request details
func SecureWithConfig ¶
func SecureWithConfig(config SecureConfig) mux.MiddlewareFunc
SecureWithConfig 返回一个带配置的 Secure 中间件。
func SetHandlerResponse ¶
Types ¶
type BodyLimitConfig ¶
type BodyLimitConfig struct {
// Limit 指定了请求体的最大允许大小
// 可以使用如 "4x" 或 "4xB" 的格式,其中x可以是K, M, G, T 或 P
Limit string
// contains filtered or unexported fields
}
BodyLimitConfig 定义了BodyLimit中间件的配置
type CSRFConfig ¶
type CSRFConfig struct {
// Skipper 定义一个函数来跳过中间件。
Skipper func(r *http.Request) bool
// TokenLength 是生成的令牌的长度。
TokenLength uint8
// TokenLookup 是一个字符串,用于从请求中提取令牌。
TokenLookup string
// ContextKey 是用于将生成的 CSRF 令牌存储到上下文中的键。
ContextKey string
// CookieName 是 CSRF cookie 的名称。
CookieName string
// CookieDomain 是 CSRF cookie 的域。
CookieDomain string
// CookiePath 是 CSRF cookie 的路径。
CookiePath string
// CookieMaxAge 是 CSRF cookie 的最大年龄(以秒为单位)。
CookieMaxAge int
// CookieSecure 指示 CSRF cookie 是否安全。
CookieSecure bool
// CookieHTTPOnly 指示 CSRF cookie 是否为 HTTP only。
CookieHTTPOnly bool
// CookieSameSite 指示 CSRF cookie 的 SameSite 模式。
CookieSameSite http.SameSite
// ErrorHandler 定义一个用于返回自定义错误的函数。
ErrorHandler func(err error, w http.ResponseWriter, r *http.Request)
}
CSRFConfig 定义 CSRF 中间件的配置。
type DefaultHandlerResponse ¶
type DefaultHandlerResponse struct {
Code int `json:"code" dc:"Error code"`
Message string `json:"message" dc:"Error message"`
Data interface{} `json:"data" dc:"Result data for certain request according API definition"`
}
DefaultHandlerResponse is the default implementation of HandlerResponse.
type SecureConfig ¶
type SecureConfig struct {
// Skipper 定义一个函数来跳过中间件。
Skipper func(r *http.Request) bool
// XSSProtection 提供对跨站脚本攻击(XSS)的保护。
XSSProtection string
// ContentTypeNosniff 提供对覆盖 Content-Type 头的保护。
ContentTypeNosniff string
// XFrameOptions 用于指示浏览器是否应该被允许在 frame 中渲染页面。
XFrameOptions string
// HSTSMaxAge 设置 Strict-Transport-Security 头。
HSTSMaxAge int
// HSTSExcludeSubdomains 在 Strict Transport Security 头中不包含 subdomains 标签。
HSTSExcludeSubdomains bool
// ContentSecurityPolicy 设置 Content-Security-Policy 头。
ContentSecurityPolicy string
// CSPReportOnly 使用 Content-Security-Policy-Report-Only 头。
CSPReportOnly bool
// HSTSPreloadEnabled 在 Strict Transport Security 头中添加 preload 标签。
HSTSPreloadEnabled bool
// ReferrerPolicy 设置 Referrer-Policy 头。
ReferrerPolicy string
}
SecureConfig 定义 Secure 中间件的配置。
type ValuesExtractor ¶
ValuesExtractor defines a function for extracting values (keys/tokens) from the given request.
func CreateExtractors ¶
func CreateExtractors(lookups string) ([]ValuesExtractor, error)
CreateExtractors creates ValuesExtractors from given lookups. Lookups is a string in the form of "<source>:<name>" or "<source>:<name>,<source>:<name>" that is used to extract key from the request. Possible values:
- "header:<name>" or "header:<name>:<cut-prefix>"
- "query:<name>"
- "param:<name>"
- "form:<name>"
- "cookie:<name>"
Multiple sources example: - "header:Authorization,header:X-Api-Key"