2

我正在努力让我的路由特定中间件与 httprouter 和 Negroni 一起使用。登录路由需要Middleware2,所有其他路由都需要Middleware1

到目前为止,我有:

func Main() {
    router := httprouter.New()
    protectedRoutes := httprouter.New()

    DefineRoutes(router)
    DefineProtectedRoutes(protectedRoutes)

    //This is the block that I'm unsure about
    //How can I introduce Middleware2 for a specific route?
    n := negroni.New()
    n.Use(negroni.HandlerFunc(Middleware1))
    n.UseHandler(router)


    n.Run(":5000")
}

func DefineRoutes(router *httprouter.Router) {
    router.POST("/v1/users/authenticate", UserLogin)
    router.POST("/v1/users/authorize", UserLogin)
    router.POST("/v1/users/logout", UserLogout)
}

func DefineProtectedRoutes(router *httprouter.Router) {
    router.POST("/v1/users/login", UserLogin)
}

但现在我有点卡住了,因为网站上的示例(https://github.com/codegangsta/negroni)使用标准处理程序。

4

1 回答 1

0

不确定是否有人仍在寻找答案。我认为可以通过调用 router.Lookup 函数来检索参数。这是我所做的:

_, params, _ :=  router.Lookup("GET", req.URL.Path)

其中 router 是 httprouter.Router 的一个实例

于 2016-07-20T00:04:05.813 回答