问题描述

我正在使用 mux 软件包,该软件包似乎工作得很好,除了它没有效果.似乎不支持复杂的路线,或者至少我不知道它是如何做到的.我有以下几种路线:

I'm using the mux package which seems to work quite well except that it doesn't seem to support complex routes or at least I don't get it how it does. I have several routes as following:

router := mux.NewRouter()
router.HandleFunc("/{productid}/{code}", product)
router.HandleFunc("/{user}", userHome)
router.HandleFunc("/search/price", searchPage)

所以我有两个问题:

  • 如何定义通配符路由(例如/search/price/*),以使诸如/search/price/29923/rage/200/color = red的请求能够匹配它?

  • How can I define a wildcard route such /search/price/* so that a request such /search/price/29923/rage/200/color=red can match it ?

/{productid}/{code}  true  handlerTrue  false handlerFalse 
/{productid}/{code}truehandlerTruefalsehandlerFalse
 .MatcherFunc(myfunction(ip)bool)
.MatcherFunc(myfunction(ip)bool)

当前,我正在处理处理程序中的自定义"条件.

Currently I'm handling the 'custom' conditions inside the handler.

推荐答案

您可以使用正则表达式.像

You can use regexps. Something like

router.HandleFunc(`/search/price/{rest:[a-zA-Z0-9=\-\/]+}`, searchPage)
 rest  rest  29923/rage/200/color = red 
restrest29923/rage/200/color=red

不过,您可能想要一些类似的可选参数.

You probably want some like optional arguments, though.

router.HandleFunc(`/search{price:(\/price\/[0-9]+)?}{rage:(\/rage\/[0-9]+)?}{color:(\/color=[a-z]+)?}`, searchPage)
 price ="/price/29923"  rage ="/rage/200"  color ="/color =红色" /search/price/29923/color = red  rage 
price = "/price/29923"rage = "/rage/200"color = "/color=red"/search/price/29923/color=redrage

我不太明白你的第二个问题.

I don't quite get your second question.

这篇关于golang多路复用器,路由通配符&自定义功能匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!