i'm looking at github.com/remyoudompheng/go-misc/zipfs
to serve content from a zip file in http.
this minimal example works, i can get a {FILE} contained in files/archives.zip:
http://localhost:8080/zip/{FILE}
package main
import (
"archive/zip"
"github.com/remyoudompheng/go-misc/zipfs"
"log"
"net/http"
)
func main() {
z, err := zip.OpenReader("files/archive.zip")
if err != nil {
log.Fatal(err)
}
defer z.Close()
http.Handle("/zip/", http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader))))
log.Fatal(http.ListenAndServe(":8080", nil))
}
now, suppose that i want something like http://localhost:8080/zip/{ZIPFILE}/{FILE}
i'm trying registering a func but doesn't work
package main
import (
"archive/zip"
"github.com/remyoudompheng/go-misc/zipfs"
"html"
"log"
"net/http"
"strings"
)
func servezip(res http.ResponseWriter, req *http.Request) {
zippath := "files/" + strings.Split(html.EscapeString(req.URL.Path), "/")[2] + ".zip"
z, err := zip.OpenReader(zippath)
if err != nil {
http.Error(res, err.Error(), 404)
return
}
defer z.Close()
http.StripPrefix("/zip/", http.FileServer(zipfs.NewZipFS(&z.Reader)))
}
func main() {
http.HandleFunc("/zip/", servezip)
log.Fatal(http.ListenAndServe(":8080", nil))
}
what i'm missing? can an handlefunc return an http.fileserver?