欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

gin使用中间件

发布时间:2025/6/15 编程问答 31 豆豆
生活随笔 收集整理的这篇文章主要介绍了 gin使用中间件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

gin使用中间件 

func main() {// 创建一个不包含中间件的路由器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") }

总结

以上是生活随笔为你收集整理的gin使用中间件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。