Gorilla Mux用于子路径路由

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

Gorilla Mux for sub path Routing

问题

我有以下路由:

router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1)
router.Methods("GET").Path("/my_get_01").HandlerFunc(myHandler2)

router.Methods("POST").Path("/my_post_02").HandlerFunc(myHandler3)
router.Methods("GET").Path("/my_get_02").HandlerFunc(myHandler4)

router.Methods("POST").Path("/my_post_03").HandlerFunc(myHandler5)
router.Methods("GET").Path("/my_get_03").HandlerFunc(myHandler6)

router.Methods("POST").Path("/my_post_04").HandlerFunc(myHandler7)
router.Methods("GET").Path("/my_get_04").HandlerFunc(myHandler8)

router.Methods("POST").Path("/my_post_05").HandlerFunc(myHandler9)
router.Methods("GET").Path("/my_get_05").HandlerFunc(myHandler10)

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

我想要做这样的事情:

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

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

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

谢谢,

英文:

I have the following routes:

router.Methods("POST").Path("/my_post_01").HandlerFunc(myHandler1)
router.Methods("GET").Path("/my_get_01").HandlerFunc(myHandler2)

router.Methods("POST").Path("/my_post_02").HandlerFunc(myHandler3)
router.Methods("GET").Path("/my_get_02").HandlerFunc(myHandler4)

router.Methods("POST").Path("/my_post_03").HandlerFunc(myHandler5)
router.Methods("GET").Path("/my_get_03").HandlerFunc(myHandler6)

router.Methods("POST").Path("/my_post_04").HandlerFunc(myHandler7)
router.Methods("GET").Path("/my_get_04").HandlerFunc(myHandler8)

router.Methods("POST").Path("/my_post_05").HandlerFunc(myHandler9)
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:

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

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

路由器

package router

var Router = mux.NewRouter()
// 处理 "/my/" 路由
var MyRouter = Router.PathPrefix("/my").Subrouter()

另一个包

import "/path/to/router"

func init() {
    router.MyRouter.Methods("POST").Path("/post_01").HandlerFunc(myHandler1)
}

在主函数中

import "/path/to/router"

func main() {
    http.Handle("/", router.Router)
    
    //...
}
英文:

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

Router

package router

var Router = mux.NewRouter()
// handle "/my/" routes
var MyRouter = Router.PathPrefix("/my").Subrouter()

another package

import "/path/to/router"

func init() {
    router.MyRouter.Methods("POST").Path("/post_01").HandlerFunc(myHandler1)
}

In main

import "/path/to/router"

func main() {
    http.Handle("/", router.Router)
    
    //...
}

答案2

得分: 2

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

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

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

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

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

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

文档: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:

r := mux.NewRouter()
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:

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

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:

确定