Documentation
¶
Overview ¶
Package middleware is gin middleware plugin.
Index ¶
- Constants
- Variables
- func AdaptCtx(ctx context.Context) (*gin.Context, context.Context)
- func Auth(opts ...AuthOption) gin.HandlerFunc
- func CircuitBreaker(opts ...CircuitBreakerOption) gin.HandlerFunc
- func Cors() gin.HandlerFunc
- func CtxRequestID(ctx context.Context) string
- func CtxRequestIDField(ctx context.Context) zap.Field
- func GCtxRequestID(c *gin.Context) string
- func GCtxRequestIDField(c *gin.Context) zap.Field
- func GetClaims(c *gin.Context) (*jwt.Claims, bool)
- func GetFromCtx(ctx context.Context, key string) interface{}
- func GetFromHeader(ctx context.Context, key string) string
- func GetFromHeaders(ctx context.Context, key string) []string
- func HeaderRequestID(c *gin.Context) string
- func HeaderRequestIDField(c *gin.Context) zap.Field
- func Logging(opts ...Option) gin.HandlerFunc
- func RailsCookieAuthMiddleware(secretKeyBase string, cookieName string) gin.HandlerFunc
- func RateLimit(opts ...RateLimitOption) gin.HandlerFunc
- func RequestID(opts ...RequestIDOption) gin.HandlerFunc
- func SimpleLog(opts ...Option) gin.HandlerFunc
- func Timeout(d time.Duration) gin.HandlerFunc
- func Tracing(serviceName string, opts ...TraceOption) gin.HandlerFunc
- func WrapCtx(c *gin.Context) context.Context
- type AuthOption
- type CircuitBreakerOption
- type CtxKeyString
- type ExtraVerifyFn
- type Option
- type RateLimitOption
- type RequestIDOption
- type TraceOption
Constants ¶
const HeaderAuthorizationKey = "Authorization"
HeaderAuthorizationKey http header authorization key, value is "Bearer token"
Variables ¶
var ( // ContextRequestIDKey request id for context ContextRequestIDKey = "request_id" // HeaderXRequestIDKey header request id key HeaderXRequestIDKey = "X-Request-Id" )
var ErrLimitExceed = rl.ErrLimitExceed
ErrLimitExceed is returned when the rate limiter is triggered and the request is rejected due to limit exceeded.
var ErrNotAllowed = circuitbreaker.ErrNotAllowed
ErrNotAllowed error not allowed.
var RequestHeaderKey = "request_header_key"
RequestHeaderKey request header key
var RequestIDKey = CtxKeyString(ContextRequestIDKey)
RequestIDKey request_id
var WithVerify = WithExtraVerify
WithVerify alias of WithExtraVerify
Functions ¶
func AdaptCtx ¶
AdaptCtx adapt context, if ctx is gin.Context, return gin.Context and context of the transformation
func Auth ¶
func Auth(opts ...AuthOption) gin.HandlerFunc
Auth authorization middleware, support custom extra verify.
func CircuitBreaker ¶
func CircuitBreaker(opts ...CircuitBreakerOption) gin.HandlerFunc
CircuitBreaker a circuit breaker middleware
func CtxRequestID ¶
CtxRequestID get request id from context.Context
func CtxRequestIDField ¶
CtxRequestIDField get request id field from context.Context
func GCtxRequestID ¶
GCtxRequestID get request id from gin.Context
func GCtxRequestIDField ¶
GCtxRequestIDField get request id field from gin.Context
func GetFromCtx ¶
GetFromCtx get value from context
func GetFromHeader ¶
GetFromHeader get value from header
func GetFromHeaders ¶
GetFromHeaders get values from header
func HeaderRequestID ¶
HeaderRequestID get request id from the header
func HeaderRequestIDField ¶
HeaderRequestIDField get request id field from header
func RailsCookieAuthMiddleware ¶ added in v1.14.5
func RailsCookieAuthMiddleware(secretKeyBase string, cookieName string) gin.HandlerFunc
RailsCookieAuthMiddleware validates and decrypts a Rails encrypted cookie, attaches the session payload to context under key "rails_session".
func RateLimit ¶
func RateLimit(opts ...RateLimitOption) gin.HandlerFunc
RateLimit an adaptive rate limiter middleware
func RequestID ¶
func RequestID(opts ...RequestIDOption) gin.HandlerFunc
RequestID is an interceptor that injects a 'request id' into the context and request/response header of each request.
func Tracing ¶
func Tracing(serviceName string, opts ...TraceOption) gin.HandlerFunc
Tracing returns interceptor that will trace incoming requests. The service parameter should describe the name of the (virtual) server handling the request.
Types ¶
type AuthOption ¶ added in v1.12.2
type AuthOption func(*authOptions)
AuthOption set the auth options.
func WithExtraVerify ¶ added in v1.13.0
func WithExtraVerify(fn ExtraVerifyFn) AuthOption
WithExtraVerify set extra verify function
func WithReturnErrReason ¶ added in v1.13.0
func WithReturnErrReason() AuthOption
WithReturnErrReason set return error reason
func WithSignKey ¶ added in v1.13.0
func WithSignKey(key []byte) AuthOption
WithSignKey set jwt sign key
type CircuitBreakerOption ¶
type CircuitBreakerOption func(*circuitBreakerOptions)
CircuitBreakerOption set the circuit breaker circuitBreakerOptions.
func WithBreakerOption ¶ added in v1.14.3
func WithBreakerOption(opts ...circuitbreaker.Option) CircuitBreakerOption
WithBreakerOption set the circuit breaker options.
func WithDegradeHandler ¶
func WithDegradeHandler(handler func(c *gin.Context)) CircuitBreakerOption
WithDegradeHandler set degrade handler function
func WithGroup ¶
func WithGroup(g *group.Group) CircuitBreakerOption
WithGroup with circuit breaker group. Deprecated: use WithBreakerOption instead
func WithValidCode ¶
func WithValidCode(code ...int) CircuitBreakerOption
WithValidCode http code to mark failed
type ExtraVerifyFn ¶ added in v1.13.0
ExtraVerifyFn extra verify function
type Option ¶
type Option func(*options)
Option set the gin logger options.
func WithIgnoreRoutes ¶
WithIgnoreRoutes no logger content routes
func WithPrintErrorByCodes ¶ added in v1.13.0
WithPrintErrorByCodes set print error by specified codes
func WithRequestIDFromContext ¶
func WithRequestIDFromContext() Option
WithRequestIDFromContext name is field in context, default value is request_id
func WithRequestIDFromHeader ¶
func WithRequestIDFromHeader() Option
WithRequestIDFromHeader name is field in header, default value is X-Request-Id
type RateLimitOption ¶
type RateLimitOption func(*rateLimitOptions)
RateLimitOption set the rate limits rateLimitOptions.
func WithCPUQuota ¶
func WithCPUQuota(quota float64) RateLimitOption
WithCPUQuota with real cpu quota(if it can not collect from process correct);
func WithCPUThreshold ¶
func WithCPUThreshold(threshold int64) RateLimitOption
WithCPUThreshold with cpu threshold
type RequestIDOption ¶
type RequestIDOption func(*requestIDOptions)
RequestIDOption set the request id options.
func WithContextRequestIDKey ¶
func WithContextRequestIDKey(key string) RequestIDOption
WithContextRequestIDKey set context request id key, minimum length of 4
func WithHeaderRequestIDKey ¶
func WithHeaderRequestIDKey(key string) RequestIDOption
WithHeaderRequestIDKey set header request id key, minimum length of 4
type TraceOption ¶
type TraceOption func(*traceConfig)
TraceOption specifies instrumentation configuration options.
func WithPropagators ¶
func WithPropagators(propagators propagation.TextMapPropagator) TraceOption
WithPropagators specifies propagators to use for extracting information from the HTTP requests. If none are specified, global ones will be used.
func WithTracerProvider ¶
func WithTracerProvider(provider oteltrace.TracerProvider) TraceOption
WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package auth provides JWT authentication middleware for gin.
|
Package auth provides JWT authentication middleware for gin. |
Package metrics is gin metrics library, collect five metrics, "uptime", "http_request_count_total", "http_request_duration_seconds", "http_request_size_bytes", "http_response_size_bytes".
|
Package metrics is gin metrics library, collect five metrics, "uptime", "http_request_count_total", "http_request_duration_seconds", "http_request_size_bytes", "http_response_size_bytes". |