package ext import ( "io/fs" "mime" "path/filepath" "github.com/valyala/fasthttp" ) type FileSystem interface { Open(name string) (fs.File, error) } // This is a VERY simple file server. It does not take a lot into consideration // and it should only be used to return small predictable files, like in the // static folder. func FileServer(rootFS FileSystem, rootPath string) fasthttp.RequestHandler { return func(ctx *fasthttp.RequestCtx) { path := ctx.UserValue("filepath").(string) f, err := rootFS.Open(rootPath + path) if err != nil { InternalServerError(ctx, err) return } defer f.Close() m := mime.TypeByExtension(filepath.Ext(path)) ctx.SetContentType(m) ctx.SetBodyStream(f, -1) } }