我正在努力让我的路由特定的中间件与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)上的示例使用了标准的处理程序。
发布于 2016-07-20 08:04:06
不确定是否有人还在寻找答案。我认为参数可以通过调用router.Lookup函数来检索。下面是我所做的:
_, params, _ := router.Lookup("GET", req.URL.Path)其中,路由器是httprouter.Router的实例
https://stackoverflow.com/questions/30350317
复制相似问题