gin使用中间件
gin使用中間件?
func main() {// 創(chuàng)建一個不包含中間件的路由器r := gin.New()// 全局中間件// 使用 Logger 中間件r.Use(gin.Logger())// 使用 Recovery 中間件r.Use(gin.Recovery())// 路由添加中間件,可以添加任意多個r.GET("/benchmark", MyBenchLogger(), benchEndpoint)// 路由組中添加中間件// authorized := r.Group("/", AuthRequired())// exactly the same as:authorized := r.Group("/")// per group middleware! in this case we use the custom created// AuthRequired() middleware just in the "authorized" group.authorized.Use(AuthRequired()){authorized.POST("/login", loginEndpoint)authorized.POST("/submit", submitEndpoint)authorized.POST("/read", readEndpoint)// nested grouptesting := authorized.Group("testing")testing.GET("/analytics", analyticsEndpoint)}// Listen and serve on 0.0.0.0:8080r.Run(":8080") }總結(jié)