middleware

package
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultJWTSecret is the default JWT secret key
	DefaultJWTSecret = "your-secret-key-change-in-production"
)
View Source
const (
	// RequestIdKey 请求ID的HTTP头key
	RequestIdKey = "X-Request-ID"
)
View Source
const (
	// SlowRequestThreshold is the threshold for slow request logging (in milliseconds)
	SlowRequestThreshold = 3000
)

Variables

This section is empty.

Functions

func ApplyCORSHeaders

func ApplyCORSHeaders(header http.Header, r *http.Request, corsConfig *conf.Server_CORS)

ApplyCORSHeaders applies the configured CORS headers onto the response.

func CORSFilter

func CORSFilter(corsConfig *conf.Server_CORS) khttp.FilterFunc

CORSFilter returns a Kratos HTTP filter backed by server.cors config.

func DeviceMiddleware

func DeviceMiddleware(deviceConfig *conf.Device, logger log.Logger) func(http.Handler) http.Handler

DeviceMiddleware 设备加密中间件 用于处理设备客户端的请求加密/解密

func GetClientIP

func GetClientIP(ctx context.Context) string

GetClientIP retrieves client IP from context

func GetGatewayMode

func GetGatewayMode(ctx context.Context) bool

GetGatewayMode retrieves gateway mode flag from context

func GetQueryParams

func GetQueryParams(ctx context.Context) map[string]string

GetQueryParams retrieves the original query params from context.

func GetRequestHost

func GetRequestHost(ctx context.Context) string

GetRequestHost retrieves request host from context

func GetRequestURI

func GetRequestURI(ctx context.Context) string

GetRequestURI retrieves request URI from context

func GetSessionID

func GetSessionID(ctx context.Context) string

GetSessionID retrieves session ID from context

func GetSubscribeConfigByToken

func GetSubscribeConfigByToken(ctx context.Context, uc *subscriptionbiz.SubscriptionUseCase, token, userAgent string, r *http.Request) ([]byte, string, error)

GetSubscribeConfigByToken 根据token获取订阅配置 这个函数需要调用subscription service来获取配置

func GetUserAgent

func GetUserAgent(ctx context.Context) string

GetUserAgent retrieves User-Agent from context

func GetUserID

func GetUserID(ctx context.Context) int64

GetUserID retrieves user ID from context

func IsCORSPreflightRequest

func IsCORSPreflightRequest(r *http.Request) bool

IsCORSPreflightRequest reports whether the request is a browser preflight.

func JWTAuth

func JWTAuth(authConfig *conf.Server_Auth) middleware.Middleware

JWTAuth returns a JWT authentication middleware

func LegacyPathCompatFilter

func LegacyPathCompatFilter() khttp.FilterFunc

LegacyPathCompatFilter keeps old Gin-style trailing-slash API paths working.

func LegacyRouteGuardFilter

func LegacyRouteGuardFilter() khttp.FilterFunc

LegacyRouteGuardFilter blocks new-only HTTP routes that do not exist in the old project.

func Logging

func Logging(logger log.Logger) middleware.Middleware

Logging returns a logging middleware for HTTP requests

func LoggingWithBody

func LoggingWithBody(logger log.Logger) middleware.Middleware

LoggingWithBody returns a logging middleware with request/response body logging Note: This middleware logs request and response bodies, use with caution as it may impact performance

func NotifyMiddleware

func NotifyMiddleware(getPaymentConfigFunc func(ctx context.Context, token string) (*PaymentConfig, error), logger log.Logger) func(http.Handler) http.Handler

NotifyMiddleware 支付通知中间件 用于处理支付平台的回调通知

func PanDomainMiddleware

func PanDomainMiddleware(subscribeConfig *conf.Subscribe, uc *subscriptionbiz.SubscriptionUseCase, logger log.Logger) func(http.Handler) http.Handler

PanDomainMiddleware 泛域名订阅中间件 用于处理泛域名的订阅请求,例如: token.example.com

func ServerMiddleware

func ServerMiddleware(nodeSecret string, logger log.Logger) func(http.Handler) http.Handler

ServerMiddleware 服务器节点认证中间件 用于验证节点服务器的请求,通过secret_key验证

func SubscribePathCompatFilter

func SubscribePathCompatFilter(appConf *conf.Application) khttp.FilterFunc

SubscribePathCompatFilter enforces the old project's subscribe_path behavior: once a custom path is configured, only that path is allowed externally.

func TraceMiddleware

func TraceMiddleware(logger log.Logger) func(http.Handler) http.Handler

TraceMiddleware 追踪中间件 使用OpenTelemetry进行分布式追踪

func WithClientIP

func WithClientIP(ctx context.Context, clientIP string) context.Context

WithClientIP stores client IP in context.

func WithGatewayMode

func WithGatewayMode(ctx context.Context, gatewayMode bool) context.Context

WithGatewayMode stores gateway mode flag in context.

func WithQueryParams

func WithQueryParams(ctx context.Context, queryParams map[string]string) context.Context

WithQueryParams stores the original query params in context.

func WithRequestHost

func WithRequestHost(ctx context.Context, requestHost string) context.Context

WithRequestHost stores request host in context.

func WithRequestURI

func WithRequestURI(ctx context.Context, requestURI string) context.Context

WithRequestURI stores request URI in context.

func WithSessionID

func WithSessionID(ctx context.Context, sessionID string) context.Context

WithSessionID stores session ID in context

func WithUserAgent

func WithUserAgent(ctx context.Context, userAgent string) context.Context

WithUserAgent stores user agent in context.

func WithUserID

func WithUserID(ctx context.Context, userID int64) context.Context

WithUserID stores user ID in context

Types

type PaymentConfig

type PaymentConfig struct {
	Platform     string
	PaymentToken string
}

PaymentConfig 支付配置

type PaymentParams

type PaymentParams struct {
	Platform string `uri:"platform"`
	Token    string `uri:"token"`
}

PaymentParams 支付参数

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter 加密响应写入器

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter, r *http.Request, encryptionKey string, logger log.Logger) *ResponseWriter

NewResponseWriter 创建加密响应写入器

func (*ResponseWriter) CloseNotify

func (rw *ResponseWriter) CloseNotify() <-chan bool

CloseNotify 关闭通知

func (*ResponseWriter) Decrypt

func (rw *ResponseWriter) Decrypt() bool

Decrypt 解密请求体

func (*ResponseWriter) Encrypt

func (rw *ResponseWriter) Encrypt()

Encrypt 加密响应体

func (*ResponseWriter) Flush

func (rw *ResponseWriter) Flush()

Flush 刷新

func (*ResponseWriter) FlushAbort

func (rw *ResponseWriter) FlushAbort()

FlushAbort 刷新并中止

func (*ResponseWriter) Hijack

func (rw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack 劫持连接

func (*ResponseWriter) Pusher

func (rw *ResponseWriter) Pusher() (pusher http.Pusher)

Pusher 推送器

func (*ResponseWriter) Size

func (rw *ResponseWriter) Size() int

Size 获取大小

func (*ResponseWriter) Status

func (rw *ResponseWriter) Status() int

Status 获取状态码

func (*ResponseWriter) Unwrap

func (rw *ResponseWriter) Unwrap() http.ResponseWriter

Unwrap 解包

func (*ResponseWriter) Write

func (rw *ResponseWriter) Write(data []byte) (n int, err error)

Write 写入数据

func (*ResponseWriter) WriteHeader

func (rw *ResponseWriter) WriteHeader(code int)

WriteHeader 写入状态码

func (*ResponseWriter) WriteHeaderNow

func (rw *ResponseWriter) WriteHeaderNow()

WriteHeaderNow 立即写入状态码

func (*ResponseWriter) WriteString

func (rw *ResponseWriter) WriteString(s string) (n int, err error)

WriteString 写入字符串

func (*ResponseWriter) Written

func (rw *ResponseWriter) Written() bool

Written 是否已写入

Jump to

Keyboard shortcuts

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