lens @ 05a8dbf46792adfef007a0ffbcb654026db036fa

 1package ext
 2
 3import (
 4	"io/fs"
 5	"mime"
 6	"path/filepath"
 7
 8	"github.com/valyala/fasthttp"
 9)
10
11type FileSystem interface {
12	Open(name string) (fs.File, error)
13}
14
15// This is a VERY simple file server. It does not take a lot into consideration
16// and it should only be used to return small predictable files, like in the
17// static folder.
18func FileServer(rootFS FileSystem, rootPath string) fasthttp.RequestHandler {
19	return func(ctx *fasthttp.RequestCtx) {
20		path := ctx.UserValue("filepath").(string)
21
22		f, err := rootFS.Open(rootPath + path)
23		if err != nil {
24			InternalServerError(ctx, err)
25			return
26		}
27		defer f.Close()
28
29		m := mime.TypeByExtension(filepath.Ext(path))
30		ctx.SetContentType(m)
31		ctx.SetBodyStream(f, -1)
32	}
33}