我正在努力让我的路由特定中间件与 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)使用标准处理程序。