使用Gorilla MUX和Negroni进行子路由中间件的设置。

huangapple go评论73阅读模式
英文:

Subroutes middlewares with Gorilla MUX and Negroni

问题

我正在尝试在某些路由上添加中间件。我写了这段代码:

func main() {
    router := mux.NewRouter().StrictSlash(false)

    admin_subrouter := router.PathPrefix("/admin").Subrouter()

    //handlers.CombinedLoggingHandler来自gorilla/handlers
    router.PathPrefix("/admin").Handler(negroni.New(
        negroni.Wrap(handlers.CombinedLoggingHandler(os.Stdout, admin_subrouter)),
    ))

    admin_subrouter.HandleFunc("/articles/new", articles_new).Methods("GET")
    admin_subrouter.HandleFunc("/articles", articles_index).Methods("GET")
    admin_subrouter.HandleFunc("/articles", articles_create).Methods("POST")

    n := negroni.New()
    n.UseHandler(router)
    http.ListenAndServe(":3000", n)
}

我希望只在以/admin为前缀的路径上看到请求日志。当我执行"GET /admin"时,我确实看到了一行日志,但当我执行"GET /admin/articles/new"时,却没有日志。我尝试了其他组合,但仍然无法解决。我的代码有什么问题?

我看到其他方法,比如在每个路由定义中包装HandlerFunc,但我希望只为前缀或子路由执行一次。

我在那里使用的日志中间件是用于测试的,也许使用身份验证中间件更有意义,但我只是想让它工作。

谢谢!

英文:

I'm trying to add a middleware only on some routes. I wrote this code:

func main() {
  router := mux.NewRouter().StrictSlash(false)

  admin_subrouter := router.PathPrefix("/admin").Subrouter()

  //handlers.CombinedLoggingHandler comes from gorilla/handlers
  router.PathPrefix("/admin").Handler(negroni.New(
	negroni.Wrap(handlers.CombinedLoggingHandler(os.Stdout, admin_subrouter)),
  ))

  admin_subrouter.HandleFunc("/articles/new", articles_new).Methods("GET")
  admin_subrouter.HandleFunc("/articles", articles_index).Methods("GET")
  admin_subrouter.HandleFunc("/articles", articles_create).Methods("POST")

  n := negroni.New()
  n.UseHandler(router)
  http.ListenAndServe(":3000", n)

}

I expect to see request logs only for paths with prefix /admin. I do see a log line when I do "GET /admin", but not when I do "GET /admin/articles/new". I tried by brute force other combinations but I can't get it. What's wrong with my code?

I saw other ways, like wrapping the HandlerFunc on each route definition, but I wanted to do it once for a prefix or a subrouter.

The logging middleware I'm using there is for testing, maybe an Auth middleware makes more sense, but I just wanted to make it work.

Thanks!

答案1

得分: 2

问题是你创建子路由/admin的方式。完整的参考代码在这里https://play.golang.org/p/zb_79oHJed

// Admin
adminBase := mux.NewRouter()
router.PathPrefix("/admin").Handler(negroni.New(
    // 这个日志记录器仅适用于/admin路由
    negroni.HandlerFunc(justTestLogger),
    // 在这里添加仅适用于`/admin`路由的处理程序
    negroni.Wrap(adminBase),
))

adminRoutes := adminBase.PathPrefix("/admin").Subrouter()
adminRoutes.HandleFunc("/articles/new", articleNewHandler).Methods("GET")

现在,访问这些URL。你将只看到/admin子路由的日志。

英文:

Issue is the way you create an sub routes /admin. Complete reference code is here https://play.golang.org/p/zb_79oHJed

// Admin
adminBase := mux.NewRouter()
router.PathPrefix("/admin").Handler(negroni.New(
	// This logger only applicable to /admin routes
	negroni.HandlerFunc(justTestLogger),
	// add your handlers here which is only appilcable to `/admin` routes
	negroni.Wrap(adminBase),
))

adminRoutes := adminBase.PathPrefix("/admin").Subrouter()
adminRoutes.HandleFunc("/articles/new", articleNewHandler).Methods("GET")

Now, access these URLs. You will see logs only for /admin sub routes.

huangapple
  • 本文由 发表于 2017年6月22日 03:31:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/44684788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定