【问题标题】:How to use Gin-gonic middleware with Google App Engine?如何在 Google App Engine 中使用 Gin-gonic 中间件?
【发布时间】:2017-01-13 05:44:52
【问题描述】:

我在 Google App Engine 平台上为我的网站使用 Gin-Gonic。 一切正常,但我开始需要使用一些中间件。

当我尝试使用时:

router.Use(MyMiddleware())

MyMiddleware() 返回的中间件似乎没有运行。

所以我的问题是:

  • 在使用 GAE 时是否可以使用 gin-gonic 中间件?
  • 如果是这样,我该如何实现?

谢谢!

这是我的来源:

main.go:

func init() {
    router := routes.Router()

    // Set the config to the context
    router.Use(SetConfiguration())

    http.Handle("/", router)
}

func SetConfiguration() gin.HandlerFunc {
    configuration := config.GetConfiguration()

    return func(c *gin.Context) {
        c.Set("config", configuration)
        c.Next()
    }
}

PS : routes.Router() 只需用gin.New() 设置路由器并添加一些路由。

【问题讨论】:

    标签: google-app-engine web go go-gin


    【解决方案1】:

    中间件路由应该在其他路由之前首先添加。在我使用 CORS MiddlewareAuth Middleware

    的项目中查看此文件

    https://github.com/wilsontamarozzi/panda-api/blob/master/routers/router.go

    【讨论】:

    • 嘿,真是个错误!猜猜看,我是在阅读 Gin 代码时才发现这一点的,然后就来这里回答自己。非常感谢您的回答!我认为应该更好地记录这件事......
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2017-08-03
    • 1970-01-01
    • 2017-07-01
    • 2021-02-28
    • 2010-11-20
    • 2017-12-04
    • 1970-01-01
    相关资源
    最近更新 更多