Gorilla Mux用于子路径路由

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

Gorilla Mux for sub path Routing

问题

我有以下路由:

  1. router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1)
  2. router.Methods("GET").Path("/my_get_01").HandlerFunc(myHandler2)
  3. router.Methods("POST").Path("/my_post_02").HandlerFunc(myHandler3)
  4. router.Methods("GET").Path("/my_get_02").HandlerFunc(myHandler4)
  5. router.Methods("POST").Path("/my_post_03").HandlerFunc(myHandler5)
  6. router.Methods("GET").Path("/my_get_03").HandlerFunc(myHandler6)
  7. router.Methods("POST").Path("/my_post_04").HandlerFunc(myHandler7)
  8. router.Methods("GET").Path("/my_get_04").HandlerFunc(myHandler8)
  9. router.Methods("POST").Path("/my_post_05").HandlerFunc(myHandler9)
  10. router.Methods("GET").Path("/my_get_05").HandlerFunc(myHandler10)

随着路由越来越多,管理起来变得越来越困难。

我想要做这样的事情:

  1. router.Path("/my/*").HandleFunc(mypackage.RegisterHandler)

所有的处理程序都分离在另一个包中。

有没有办法在另一个包中匹配这些路径?

谢谢,

英文:

I have the following routes:

  1. router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1)
  2. router.Methods("GET").Path("/my_get_01").HandlerFunc(myHandler2)
  3. router.Methods("POST").Path("/my_post_02").HandlerFunc(myHandler3)
  4. router.Methods("GET").Path("/my_get_02").HandlerFunc(myHandler4)
  5. router.Methods("POST").Path("/my_post_03").HandlerFunc(myHandler5)
  6. router.Methods("GET").Path("/my_get_03").HandlerFunc(myHandler6)
  7. router.Methods("POST").Path("/my_post_04").HandlerFunc(myHandler7)
  8. router.Methods("GET").Path("/my_get_04").HandlerFunc(myHandler8)
  9. router.Methods("POST").Path("/my_post_05").HandlerFunc(myHandler9)
  10. router.Methods("GET").Path("/my_get_05").HandlerFunc(myHandler10)

As I have more and more routes, it gets harder to manage.

I want to do something like:

  1. router.Path("/my/*").HandleFunc(mypackage.RegisterHandler)

with all handlers being separated in another package

Is there any way that I can match those paths in a separate package?

Thanks,

答案1

得分: 2

你可以为你的路由器创建一个包,然后导入该包并添加你的路由。

路由器

  1. package router
  2. var Router = mux.NewRouter()
  3. // 处理 "/my/" 路由
  4. var MyRouter = Router.PathPrefix("/my").Subrouter()

另一个包

  1. import "/path/to/router"
  2. func init() {
  3. router.MyRouter.Methods("POST").Path("/post_01").HandlerFunc(myHandler1)
  4. }

在主函数中

  1. import "/path/to/router"
  2. func main() {
  3. http.Handle("/", router.Router)
  4. //...
  5. }
英文:

You could create a package for your router then import said package and add your routes.

Router

  1. package router
  2. var Router = mux.NewRouter()
  3. // handle "/my/" routes
  4. var MyRouter = Router.PathPrefix("/my").Subrouter()

another package

  1. import "/path/to/router"
  2. func init() {
  3. router.MyRouter.Methods("POST").Path("/post_01").HandlerFunc(myHandler1)
  4. }

In main

  1. import "/path/to/router"
  2. func main() {
  3. http.Handle("/", router.Router)
  4. //...
  5. }

答案2

得分: 2

如果你能从请求URL中提取id并在通用处理程序中处理它,那将会更好。

实际上,你离目标并不远,只需修改你的路由器如下:

  1. r := mux.NewRouter()
  2. r.Methods("POST").HandleFunc("/articles/{article_id:[0-9]+}", ArticlePostHandler)

article_id是参数名,[0-9]+是用于匹配它的正则表达式。

ArticlePostHandler中(你可以从另一个包中导入它),使用mux.Vars来获取id,像这样:

  1. func ArticlePostHandler(resp http.ResponseWriter, req *http.Request) {
  2. articleId := mux.Vars(req)["article_id"]
  3. // 根据需要处理`articleId`
  4. }

文档:http://www.gorillatoolkit.org/pkg/mux

英文:

It would be a lot better if you could extract id from the request URL and handle it in a generic handler.

Actually, it is not far from where you are right now, modify you router like this:

  1. r := mux.NewRouter()
  2. r.Methods("POST").HandleFunc("/articles/{article_id:[0-9]+}", ArticlePostHandler)

article_id is the parameter name and [0-9]+ is the regexp to match it.

And in the ArticlePostHandler (you could import it from another package), use mux.Vars to get the id, like this:

  1. func ArticlePostHandler(resp http.ResponseWriter, req *http.Request) {
  2. articleId := mux.Vars(req)["article_id"]
  3. // do whatever you want with `articleId`
  4. }

Document: http://www.gorillatoolkit.org/pkg/mux

huangapple
  • 本文由 发表于 2015年2月6日 02:11:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/28351063.html
匿名

发表评论

匿名网友

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

确定