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

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改