使用文件服务器从Rice Box中提供favicon.icon

问题描述 投票:0回答:1

我一直在努力解决这个问题。如何从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.
http go
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.