获取当前匹配的 http.HandleFunc 的模式。

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

Get the current pattern that matched an http.HandleFunc

问题

有没有办法获取触发http.HandleFunc的当前路由?类似这样的方式吗?

  1. http.HandleFunc("/foo/", serveFoo)
  2. func serveFoo(rw http.ResponseWriter, req *http.Request) {
  3. fmt.Println(http.CurrentRoute())
  4. // 应该打印出"/foo/"
  5. }

我想要获取当前路由的原因是因为我经常写这样的代码。

  1. if req.URL.Path != "/some-route/" {
  2. http.NotFound(resp, req)
  3. return
  4. }
  5. // 或者
  6. key := req.URL.Path[len("/some-other-route/"):]

如果代码能更容易复制粘贴、模块化和DRY(Don't Repeat Yourself),那就更好了。

  1. if req.URL.Path != http.CurrentRoute() {
  2. http.NotFound(resp, req)
  3. return
  4. }
  5. // 或者
  6. key := req.URL.Path[http.CurrentRoute():]

这只是一个小问题,所以我不想在我的项目中引入一个完整的依赖(Gorilla Mux)。

英文:

Is there a way to get the current route that triggered an http.HandleFunc? Maybe something like this?

  1. http.HandleFunc("/foo/", serveFoo)
  2. func serveFoo(rw http.ResponseWriter, req *http.Request) {
  3. fmt.Println(http.CurrentRoute())
  4. // Should print "/foo/"
  5. }

The reason I want to get the current route is because I find myself writing code like this often.

  1. if req.URL.Path != "/some-route/" {
  2. http.NotFound(resp, req)
  3. return
  4. }
  5. // or
  6. key := req.URL.Path[len("/some-other-route/"):]

It would be nice if the code was a bit more copy-pastable, modular, and DRY like this.

  1. if req.URL.Path != http.CurrentRoute() {
  2. http.NotFound(resp, req)
  3. return
  4. }
  5. // or
  6. key := req.URL.Path[http.CurrentRoute():]

This is really just a small thing, so I'd rather not bring a whole other dependency into my project (Gorilla Mux).

答案1

得分: 1

无法获取匹配的当前路由,但可以在您的场景中消除重复的代码。编写一个处理程序,在调用另一个处理程序之前检查路径:

  1. func HandleFuncExact(mux *http.ServeMux, pattern string, handler func(http.ResponseWriter, *http.Request)) {
  2. mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
  3. if req.URL.Path != pattern {
  4. http.NotFound(w, r)
  5. return
  6. }
  7. handler(w, r)
  8. })
  9. }

在您的应用程序中,调用包装函数而不是HandleFunc

  1. HandleFuncExact(http.DefaultServeMux, "/some-route/", serveSomeRoute)

函数serveSomeRoute可以假设路径完全是"/some-route/"

英文:

It is not possible to get the current route that matched, but it is possible to eliminate the duplicate code in your scenario. Write a handler that checks the path before calling through to another handler:

  1. func HandleFuncExact(mux *http.ServeMux, pattern string, handler func(http.ResponseWriter, *http.Request) {
  2. mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
  3. if req.URL.Path != pattern {
  4. http.NotFound(w, r)
  5. return
  6. }
  7. handler(w, r)
  8. })
  9. }

In your application, call the wrapper instead of HandlFunc:

  1. HandleFuncExact(http.DefaultServeMux, "/some-route/", serveSomeRoute)

The function serveSomeRoute can assume that the path is exactly "/some-route/".

huangapple
  • 本文由 发表于 2015年3月26日 08:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/29268860.html
匿名

发表评论

匿名网友

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

确定