我正在努力让我的路由特定中间件与 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) 上的示例使用的是标准处理程序。
最佳答案
不确定是否有人仍在寻找答案。我认为可以通过调用 router.Lookup 函数来检索参数。这是我所做的:
_, params, _ := router.Lookup("GET", req.URL.Path)
其中 router 是 httprouter.Router 的一个实例
关于go - Negroni 特定于路由的中间件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30350317/