middleware

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: MIT Imports: 30 Imported by: 0

README

Gin 自带的中间件有很多种,可以在 https://github.com/gin-gonic/contrib 找到。

下面是一些常用的中间件

  • RestGate:REST API 端点的安全身份验证
  • gin-jwt:用于 Gin 框架的 JWT 中间件
  • gin-sessions:基于 MongoDB 和 MySQL 的会话中间件
  • gin-location:用于公开服务器主机名和方案的中间件
  • gin-nice-recovery:异常错误恢复中间件,让您构建更好的用户体验
  • gin-limit:限制同时请求,可以帮助解决高流量负载
  • gin-oauth2:用于处理 OAuth2
  • gin-template:简单易用的 Gin 框架 HTML/模板
  • gin-redis-ip-limiter:基于 IP 地址的请求限制器
  • gin-access-limit:通过指定允许的源 CIDR 表示法来访问控制中间件
  • gin-session:Gin 的会话中间件
  • gin-stats:轻量级且有用的请求指标中间件
  • gin-session-middleware:一个高效,安全且易于使用的 Go 会话库
  • ginception:漂亮的异常页面
  • gin-inspector:用于调查 HTTP 请求的 Gin 中间件

Reference

Documentation

Index

Constants

View Source
const (
	// ContextRequestIDKey context request id for context
	ContextRequestIDKey = "request_id"

	// HeaderXRequestIDKey http header request ID key
	HeaderXRequestIDKey = "X-Request-ID"
)

Variables

View Source
var ErrLimitExceed = errors.New("[RATELIMIT] service unavailable due to rate limit exceeded")

ErrLimitExceed is service unavailable due to rate limit exceeded.

View Source
var ErrNotAllowed = errors.New("[BREAKER] request failed due to circuit breaker is open")

ErrNotAllowed is request failed due to circuit breaker triggered.

View Source
var Middlewares = defaultMiddlewares()

Middlewares global middleware

Functions

func AccessLog

func AccessLog() gin.HandlerFunc

AccessLog record access log.

func Auth

func Auth(paths ...string) gin.HandlerFunc

Auth authorize user

func Breaker

func Breaker(opts ...BreakerOption) gin.HandlerFunc

Breaker a circuit breaker middleware

func Cors

func Cors() gin.HandlerFunc

Cors add cors headers.

func GetRequestIDFromContext

func GetRequestIDFromContext(c *gin.Context) string

GetRequestIDFromContext returns 'RequestID' from the given context if present.

func GetRequestIDFromHeaders

func GetRequestIDFromHeaders(c *gin.Context) string

GetRequestIDFromHeaders returns 'RequestID' from the headers if present.

func Logging

func Logging() gin.HandlerFunc

Logging is a middleware function that logs the each request.

func Metrics

func Metrics(serviceName string) gin.HandlerFunc

Metrics returns a gin.HandlerFunc for exporting some Web metrics

func NoCache

func NoCache(c *gin.Context)

NoCache is a middleware function that appends headers to prevent the client from caching the HTTP response.

func Options

func Options(c *gin.Context)

Options is a middleware function that appends headers for options requests and aborts then exits the middleware chain and ends the request.

func Ratelimit

func Ratelimit(opts ...LimiterOption) gin.HandlerFunc

Ratelimit a circuit breaker middleware

func RequestID

func RequestID() gin.HandlerFunc

RequestID is a middleware that injects a 'X-Request-ID' into the context and request/response header of each request.

func Secure

func Secure(c *gin.Context)

Secure is a middleware function that appends security and resource access headers.

func SignMd5Middleware

func SignMd5Middleware() gin.HandlerFunc

SignMd5Middleware md5 签名校验中间件

func Timeout

func Timeout(t time.Duration) gin.HandlerFunc

Timeout 超时中间件

func Tracing

func Tracing(serviceName string, opts ...Option) gin.HandlerFunc

Tracing returns middleware that will trace incoming requests. The service parameter should describe the name of the (virtual) server handling the request.

Types

type BreakerOption

type BreakerOption func(*options)

BreakerOption is circuit breaker option.

func WithGroup

func WithGroup(g *group.Group) BreakerOption

WithGroup with circuit breaker group. NOTE: implements generics circuitbreaker.CircuitBreaker

type LimiterOption

type LimiterOption func(*limiterOptions)

LimiterOption is ratelimit option.

func WithLimiter

func WithLimiter(limiter ratelimit.Limiter) LimiterOption

WithLimiter set Limiter implementation, default is bbr limiter

type Option

type Option func(*config)

Option specifies instrumentation configuration options.

func WithPropagators

func WithPropagators(propagators propagation.TextMapPropagator) Option

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) Option

WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.

Jump to

Keyboard shortcuts

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