我想为不同的路径使用不同的中间件。我当前的实现来自这个链接
UserRouter := mux.NewRouter().StrictSlash(true)
AdminRouter := mux.NewRouter().StrictSlash(true)
Router.HandleFunc("/apps/{app_name}/xyz", Handler).Methods("GET")
我创建了三个不同的路由器,以便我可以将它们与不同的路径和中间件相关联
nUserPath := negroni.New(middleware.NewAuthMiddleWare())
nUserPath.UseHandler(UserRouter)
nAdminPath := negroni.New()
nAdminPath.UseHandler(AdminRouter)
我创建了两个不同的 negroni 实例并将它们传递给各自的路由器。因为我希望所有这些都在同一个端口上运行同一个应用程序的一部分,所以我创建了一个 Wrapper Router 和 negroni 实例,并将它们与下面的现有实例相关联
BaseRouter := mux.NewRouter().StrictSlash(true)
BaseRouter.Handle(UserBasePath,nUserPath) // UserBasePath is `/apps`
BaseRouter.Handle(HealthCheck,nUserPath) // HealthCheck is `/health`
BaseRouter.Handle(AdminBasePath,nAdminPath) // AdminBasePath is `/Admin`
n := negroni.New(middleware.NewLogger()) // attached other common middleware here
n.UseHandler(router.BaseRouter)
n.Run(":8080")
这种方法面临的问题:
当我运行/health时它运行正常,但是当我运行时/apps/{app_name}/something我得到一个404: Not Found
注意:我经历了下面链接中提到的其他方法,但它们不能满足我的需要。