xhandler

package
v0.0.0-...-b147793 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessLog

type AccessLog struct {
	// Logger 必填,打印日志的 logger
	Logger xlog.Logger

	// OnCookies 可选,处理 Cookie
	OnCookies func(cookies []*http.Cookie) []xlog.Attr

	// OnHeaders 可选,处理 Header
	OnHeaders func(h http.Header) []xlog.Attr

	// OnPanic 可选,panic 后,自定义输出
	OnPanic func(w http.ResponseWriter, r *http.Request, re any)

	// OnRequest 可选,ctx 日志字段初始化完成后,在执行后续 ServeHTTP 方法前调用
	OnRequest func(ctx context.Context, r *http.Request)

	// RePanic 当 panic 发生后,是否将 panic 重新抛出
	RePanic bool
}

AccessLog 打印访问日志

func (*AccessLog) Next

func (al *AccessLog) Next(handler http.Handler) http.Handler

type AntiTheft

type AntiTheft struct {
	// AllowDomain  有效的域名,如 example.com,则允许此域名以及所其子域名来源的 refer
	AllowDomain []string

	xlog.WithLogger

	// Forbidden 当判断无效请求时,的回调 handler,可选
	Forbidden http.Handler
	// contains filtered or unexported fields
}

AntiTheft 使用 refer 信息判断请求来源是否有效

func (*AntiTheft) Next

func (a *AntiTheft) Next(handler http.Handler) http.Handler

func (*AntiTheft) Status

func (a *AntiTheft) Status() map[string]any

type Cache

type Cache struct {
	// Store 必填,缓存对象
	Store xcache.Cache[string, string]

	// Key 必填,缓存的 key,在 Handler 未执行前执行
	// 返回值的第一个参数是缓存的 key,第二个参数是缓存有效期,若为 0 则不缓存
	Key func(w http.ResponseWriter, r *http.Request) (string, time.Duration)

	// Log 是否添加日志状态字段,可选
	// 日志字段名为 xch,可选值:n-无缓存,r-读取并使用缓存,w-写缓存
	Log bool
}

Cache 给 GET 请求添加缓存

func (*Cache) Next

func (c *Cache) Next(handler http.Handler) http.Handler

type ETag

type ETag struct {
	// Can 可选,用于判断当前请求是否需要添加 etag, 在业务 handler 执行前执行
	// 若为 nil,则跳过此判断
	Can func(w http.ResponseWriter, r *http.Request) bool
}

ETag 给所有 GET 请求,并且statusCode=200 的响应添加 etag 标记。 若请求携带 If-None-Match,并且和实际的 etag 一直,则直接发送 304 响应,不再发送 Body

func (*ETag) Next

func (e *ETag) Next(handler http.Handler) http.Handler

Jump to

Keyboard shortcuts

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