dongzuo7166 2016-03-20 09:33
浏览 73
已采纳

将go.rice与http.ServeFile()一起使用

Maybe I'm looking at the wrong places, but I cannot find an example of using go.rice with http.ServeFile(). Basically what I want is serve a boxed file with http.ServeFile(). What I have now is the following. As you can see I'm looking for a way to get the string location of a boxed file, since http.ServeFile requires that. I don't know how to get it. Any suggestions?

var StaticBox *rice.Box

func NewStaticBox() {
    StaticBox = rice.MustFindBox("../../static")
}

func Static(req *http.Request, resp *http.Response) {
    stringToBoxedFile := WHAT-TO-DO-HERE
    http.ServeFile(req, resp, stringToBoxedFile)
}

I can use the rice.box in various ways. I can get a file contents as a string with StaticBox.String(), etc. But now I want a string as a "location" to a boxed file.

  • 写回答

2条回答 默认 最新

  • doukuiqian9911 2016-03-20 12:34
    关注

    You could use http.ServeContent instead. Get a HTTPBox and use HTTPBox.Open to get a http.File, which implements io.ReadSeeker, so it can be used with ServeContent.

    Alternatively, you could use HTTPBox with http.FileServer, as suggested in the documentation of go.rice (http.FileServer will get the path of the request, and use that as a filename to find the file in the box.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?