💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
``` routers := gin.New() routers.Use(gin.Recovery()) ``` ## 使用路由组中间件进行权限验证 #### 定义中间件 在 middelware/Auth.go文件中定义token验证中间件 ```go package middleware import ( "fmt" "github.com/gin-gonic/gin" ) // TOKEN验证中间件 func AuthMiddleWare() gin.HandlerFunc { return func(c *gin.Context) { defer func() { if r := recover(); r != nil { fmt.Println("error:", r) c.Abort() return } }() token := c.GetHeader("Token") if token == ""{ fmt.Println("TOKEN验证失败") c.Abort() return } fmt.Println("token", token) // 进行token验证 // ... } } ``` #### 使用路由组中间件 ```go package router import ( "github.com/gin-gonic/gin" "kefu-work/controller/v1" "kefu-work/middleware" ) func ApiInit(router *gin.Engine){ router.Use(middleware.AuthMiddleWare()) { router.Get("/article", v1.Article{}.GetList) router.POST("/article", v1.Article{}.Write) } } ```