在Go(语言)中注册处理程序时,是否可以在模式中指定通配符?
例如:
http.HandleFunc("/groups/*/people", peopleInGroupHandler)
其中*
可以是任何有效的URL字符串。还是唯一的解决方案是/groups
从处理程序(peopleInGroupHandler
)函数内部匹配并找出其余部分?
在Go(语言)中注册处理程序时,是否可以在模式中指定通配符?
例如:
http.HandleFunc("/groups/*/people", peopleInGroupHandler)
其中*
可以是任何有效的URL字符串。还是唯一的解决方案是/groups
从处理程序(peopleInGroupHandler
)函数内部匹配并找出其余部分?
Answers:
http.Handler和http.HandleFunc的模式不是正则表达式或glob。无法指定通配符。它们记录在这里。
也就是说,创建自己的可以使用正则表达式或所需的任何其他模式的处理程序并不难。这是一个使用正则表达式(已编译,但未经测试)的表达式:
type route struct {
pattern *regexp.Regexp
handler http.Handler
}
type RegexpHandler struct {
routes []*route
}
func (h *RegexpHandler) Handler(pattern *regexp.Regexp, handler http.Handler) {
h.routes = append(h.routes, &route{pattern, handler})
}
func (h *RegexpHandler) HandleFunc(pattern *regexp.Regexp, handler func(http.ResponseWriter, *http.Request)) {
h.routes = append(h.routes, &route{pattern, http.HandlerFunc(handler)})
}
func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, route := range h.routes {
if route.pattern.MatchString(r.URL.Path) {
route.handler.ServeHTTP(w, r)
return
}
}
// no pattern matched; send 404 response
http.NotFound(w, r)
}
(h *RegexpHandler) Handler
应该是(h *RegexpHandler) Handle
?在这里看到:golang.org/pkg/net/http/#ServeMux.Handle :)
net/http
。你能举一个完整的例子吗?谢谢。
自2011年以来,您现在(2014年以上)可以找到其他解决方案。
例如,大猩猩Web工具包的mux软件包提供了所有种类的路由选项:
它可以很容易地集成到任何BYOR(带上自己的路由器)http库,例如negroni。
这是文章“大猩猩与帕特与路线:多人对决”的示例:
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
)
func main() {
rtr := mux.NewRouter()
rtr.HandleFunc("/user/{name:[a-z]+}/profile", profile).Methods("GET")
http.Handle("/", rtr)
log.Println("Listening...")
http.ListenAndServe(":3000", nil)
}
func profile(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
name := params["name"]
w.Write([]byte("Hello " + name))
}
有时最好不要只使用另一个“魔术”包,而要了解幕后的情况
在这种情况下,“魔术”定义为“ gorilla/mux/regexp.go
”,并在此处进行测试。
这个想法是提取命名变量,组装要匹配的正则表达式,创建“反向”模板以构建URL,并编译正则表达式以验证URL构建中使用的变量值。
我只想添加julienschmidt/httprouter
,其行为类似于,net/http
但具有用于url值的附加参数和对请求方法的支持:
https://github.com/julienschmidt/httprouter
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
log.Fatal(http.ListenAndServe(":8080", router))
}
它似乎也比gorilla/mux
(根据GitHub)更受欢迎,并且它还声称需要更少的内存。
这是一个如何使用@evanshaw中的代码示例的示例
func handleDigits(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Digits in the URL\n"))
}
func handleStrings(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Strings in the URL\n"))
}
func main() {
handler := &RegexpHandler{}
reg1, _ := regexp.Compile("/foo-\\d+")
handler.HandleFunc(reg1, handleDigits)
reg2, _ := regexp.Compile("/foo-\\w+")
handler.HandleFunc(reg2, handleStrings)
http.ListenAndServe(":3000", handler)
}
您可以检查violetear如何处理动态+综合(通配符)模式,这仅是补充示例:
uuid := `[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}`
router.AddRegex(":uuid")
router.HandleFunc("/test/:uuid/:uuid", handleUUID, "GET,HEAD")
在这种情况下,请求可能有2个不同 UUIDS
对于动态/通配符,可以应用:
http://api.violetear.org/command/ping/127.0.0.1
\______/\___/\________/
| | |
static |
dynamic
正则表达式可用于匹配IP:
router.AddRegex(":ip", `^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$`)
router.HandleFunc("/command/ping/:ip", ipHandler, "GET")
或者只是仅捕获所有允许GET
和HEAD
方法:
router.HandleFunc("/command/ping/*", anyHandler, "GET, HEAD")
可以在这里找到更多示例:https : //violetear.org/post/how-it-works/
Beego,所有Golang Web服务器问题的答案。Wetalk是一个基于Beego的博客网站。