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.
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.)
There is actually a very simple and short solution to achieve what you want:
func main() {
box := rice.MustFindBox("../../static")
http.Handle("/", http.FileServer(box.HTTPBox()))
http.ListenAndServe(":8080", nil)
}