doubinduo3364 2018-03-02 17:42
浏览 47
已采纳

使用文件服务器从饭盒提供favicon.icon

I have been struggling with this for a bit. How do I return, from RiceBox, favicon.ico from a static directory?

Here's the code I have, including relevant parts:

mux := mux.NewServeMux()
StaticBox := rice.MustFindBox("static")
StaticFileServer := http.StripPrefix("/static/", http.FileServer(StaticBox.HTTPBox()))
mux.Handle("/static/", StaticFileServer)
mux.HandleFunc("/", Home)

Note: favicon.ico lives inside the static directory among other assets like, js/, css/, etc.

404 when fetching it:

$ 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.
  • 写回答

1条回答 默认 最新

  • dongyi5817 2018-03-03 07:53
    关注

    I wrangled this solution, hope it helps someone:

    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’
    

    Gist: https://gist.github.com/farhany/0fbf84e265ec3e6f3502675e83008149

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题