net.http.ServeMux是否可以匹配整个子树?

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

Is it possible to make net.http.ServeMux match the whole subtree?

问题

我想创建一个ServeMux来匹配整个子树。

例如:

有一个处理程序能够处理所有与用户相关的功能。
不想列出所有可能的与用户相关的URL,像这样:

mux := http.NewServeMux()  
mux.Handle("/user/", TestHandler)
mux.Handle("/user/function01", TestHandler)
mux.Handle("/user/function02", TestHandler)
mux.Handle("/user/function03/sub-function01", TestHandler)
...
...
mux.Handle("/user/function100/sub-function01", TestHandler)
mux.Handle("/user/function100/sub-function01/sub-sub-function01", TestHandler)

是否可以在一行中匹配所有以"/user"开头的URL?

英文:

I would like to create a ServeMux to match a whole subtree.

For example:

There is a handler is able to process all user-related functions.
It's not nice to list all possible user-related urls like

mux := http.NewServeMux()  
mux.Handle("/user/", TestHandler)
mux.Handle("/user/function01", TestHandler)
mux.Handle("/user/function02", TestHandler)
mux.Handle("/user/function03/sub-function01", TestHandler)
...
...
mux.Handle("/user/function100/sub-function01", TestHandler)
mux.Handle("/user/function100/sub-function01/sub-sub-function01", TestHandler)

Is it possible to match all urls which starts with '/user' in a single line ?

答案1

得分: 4

它将自动调用/user/处理程序,如果路径不存在,例如:示例

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/user/", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "generic user handler: %v\n", req.URL.Path)
    })
    mux.HandleFunc("/user/f1/", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "f1 generic handler, %v\n", req.URL.Path)
    })
    mux.HandleFunc("/user/f1/sf1", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "sf1 generic handler, %v\n", req.URL.Path)
    })
    fmt.Println(http.ListenAndServe(":9020", mux))
}

输出结果:

➜ curl localhost:9020/user/
generic user handler: /user/
➜ curl localhost:9020/user/f1/
f1 generic handler, /user/f1/
➜ curl localhost:9020/user/f1/sf1
sf1 generic handler, /user/f1/sf1
➜ curl localhost:9020/user/f2
generic user handler: /user/f2
➜ curl localhost:9020/user/f1/sf2
f1 generic handler, /user/f1/sf2
英文:

It will automaticly call the /user/ handler if the path doesn't exist, for example:

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/user/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "generic user handler: %v\n", req.URL.Path)
	})
	mux.HandleFunc("/user/f1/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "f1 generic handler, %v\n", req.URL.Path)
	})
	mux.HandleFunc("/user/f1/sf1", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "sf1 generic handler, %v\n", req.URL.Path)
	})
	fmt.Println(http.ListenAndServe(":9020", mux))
}

Produces:

➜ curl localhost:9020/user/
generic user handler: /user/
➜ curl localhost:9020/user/f1/
f1 generic handler, /user/f1/
➜ curl localhost:9020/user/f1/sf1
sf1 generic handler, /user/f1/sf1
➜ curl localhost:9020/user/f2
generic user handler: /user/f2
➜ curl localhost:9020/user/f1/sf2
f1 generic handler, /user/f1/sf2

huangapple
  • 本文由 发表于 2014年6月27日 00:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/24435393.html
匿名

发表评论

匿名网友

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

确定