我一直在努力解决这个问题。如何从RiceBox,从静态目录返回favicon.ico?
这是我的代码,包括相关部分:
mux := mux.NewServeMux()
StaticBox := rice.MustFindBox("static")
StaticFileServer := http.StripPrefix("/static/", http.FileServer(StaticBox.HTTPBox()))
mux.Handle("/static/", StaticFileServer)
mux.HandleFunc("/", Home)
注意:favicon.ico位于静态目录中,包括js /,css /等其他资源。
获取时<404>:$ wget http://localhost:9000/favicon.ico
--2018-03-02 09:40:08-- http://localhost:9000/favicon.ico
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|127.0.0.1|:9000... connected.
HTTP request sent, awaiting response... 404 Not Found
2018-03-02 09:40:08 ERROR 404: Not Found.
答案 0 :(得分:0)
我争论这个解决方案,希望它可以帮助某人:
func FavIcon(w http.ResponseWriter, r *http.Request) {
StaticBox := rice.MustFindBox("static")
faviconContent, _ := StaticBox.HTTPBox().Open(r.URL.Path)
http.ServeContent(w, r, r.URL.Path, time.Now(), faviconContent)
}
$ wget http://127.0.0.1:9000/favicon.ico
--2018-03-02 23:44:01-- http://127.0.0.1:9000/favicon.ico
Connecting to 127.0.0.1:9000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1150 (1.1K) [image/x-icon]
Saving to: ‘favicon.ico’
要点:https://gist.github.com/farhany/0fbf84e265ec3e6f3502675e83008149