middleware

package
v0.0.0-...-49bf833 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 License: MIT Imports: 9 Imported by: 0

README

Web 中间件

  • HTTP / RPC 请求必经的一个中间层,该中间层可以统一处理所有的请求
  • Gin 的中间件是一个 HandlerFunc,它的签名是 func(c *gin.Context)
    • 基于洋葱模型,中间件可以在请求前后做一些事情
      • 请求前置拦截: Web 请求到达我们定义的 HTTP 请求处理方法之前,拦截请求并进行相应处理
      • 请求后置拦截: 在处理完成请求并响应客户端时,拦截响应并进行相应的处理
    • mw模型

1. 自定义中间件

  • RequestID()
    • 在每一个 HTTP 请求的 context, response 中注入 X-Request-ID 字段
      • 注入上下文有助于服务器端的处理过程中跟踪请求
      • 注入响应头有助于客户端获取请求的唯一标识
    • 其作用是为每一个请求生成一个唯一的 ID,方便日志追踪

2. Gin 中间件

  • NoCache()
    • 禁用客户端缓存 HTTP 请求的响应
  • Cors
    • 处理浏览器的跨域请求
  • Secure
    • 添加 HTTP 安全相关的响应头

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Authn

func Authn() gin.HandlerFunc

Authn is an authentication Gin middleware used to extract and validate a token from gin.Context If the token is valid, it sets the `sub` from the token as the <username> in gin.Context's XUsernameKey key

func Authz

func Authz(a Auther) gin.HandlerFunc

Authz is the middleware that authorizes the parsed request path with casbin

func Cors

func Cors(c *gin.Context)

Cors is a Gin middleware used to set the headers for OPTIONS requests then exit the middleware chain and complete the request (for handling browser cross-origin requests).

func NoCache

func NoCache(c *gin.Context)

NoCache is a Gin middleware used to disable client-side caching of HTTP request response

func RequestID

func RequestID() gin.HandlerFunc

RequestID is a Gin middleware used to inject the `X-Request-ID` key-value pair into the context and response of each HTTP request

func Secure

func Secure(c *gin.Context)

Secure is a Gin middleware used to add HTTP headers related to security and resource access

Types

type Auther

type Auther interface {
	Authorize(sub, obj, act string) (bool, error)
}

Jump to

Keyboard shortcuts

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