英文:
gorilla mux excluding request by extension
问题
尝试使用github.com/gorilla/mux
配置Go服务器路由,以便对所有请求都响应index.html,但排除具有.jpg | .js | .png扩展名的请求。
由于扩展名被排除,静态文件将被路由到配置的FileServer。
失败的尝试
func main() {
r := mux.NewRouter()
r.HandleFunc("/{path:^.*([!js|jpg|png|gif])$}", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "dist/index.html")
})
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("dist"))))
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
英文:
An attempt to configure go server routing with github.com/gorilla/mux
to respond to all requests with index.html but exclude requests with extension .jpg|.js|.png
Static files excluded due to extension will be routed to FileServer. configured.
Failed Attempt
func main() {
r := mux.NewRouter()
r.HandleFunc("/{path:^.*([!js|jpg|png|gif])$}", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "dist/index.html")
})
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("dist"))))
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
答案1
得分: 3
更好的方法欢迎,希望使用正则表达式,这样就可以保留原样,而不需要疯狂的if/else条件。
func main() {
r := mux.NewRouter()
r.HandleFunc("/{path:.*}", func(w http.ResponseWriter, r *http.Request) {
if HasSuffix(r.URL.Path, []string{"js", "css", "gif", "jpeg", "woff2", "woff", "ttf"}) == false {
fmt.Println("serving index")
http.ServeFile(w, r, "dist/index.html")
} else {
http.ServeFile(w, r, "dist/"+r.URL.Path)
}
})
//r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("dist"))))
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
//HasSuffix检查URL是否具有后缀
func HasSuffix(path string, parts []string) bool {
for _, part := range parts {
fmt.Println("checking if part:" + part + " exists in path:" + path)
if strings.HasSuffix(path, part) == true {
return true
}
}
return false
}
英文:
Better approach welcome, hoped to use regular expression so things where left intact with no crazy if/else conditions
func main() {
r := mux.NewRouter()
r.HandleFunc("/{path:.*}", func(w http.ResponseWriter, r *http.Request) {
if HasSuffix(r.URL.Path, []string{"js", "css", "gif", "jpeg", "woff2", "woff", "ttf"}) == false {
fmt.Println("serving index")
http.ServeFile(w, r, "dist/index.html")
} else {
http.ServeFile(w, r, "dist/"+r.URL.Path)
}
})
//r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.Dir("dist"))))
http.Handle("/", r)
http.ListenAndServe(":8000", nil)
}
//HasSuffix check if url has suffix
func HasSuffix(path string, parts []string) bool {
for _, part := range parts {
fmt.Println("checking if part:" + part + " exists in path:" + path)
if strings.HasSuffix(path, part) == true {
return true
}
}
return false
}
答案2
得分: 1
这个"^.*([!js|jpg|png|gif])$}"
不是一个有效的正则表达式,用于匹配不包含.jpg|.js|.png的字符串。
然而,在golang中,由于技术原因,不支持Negative lookahead,具体原因是它与库的O(n)时间保证冲突。
我建议你换一种方式,即为png、js、css文件等添加处理程序,以便按原样提供这些文件。
英文:
This "^.*([!js|jpg|png|gif])$}"
is not a valid regular expession for matching string that does not have .jpg|.js|.png
However, In golang Negative lookahead isn't supported for technical reasons, specifically because it conflicts with the O(n)-time guarantees of the library.
I would suggest you to do it other way around ie, add handlers for png,js,css files etc to serve the files as such
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论