英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论