goveal/api/reveal.go
Peter Kurfer 38471371c9
All checks were successful
agola/goveal/Test and lint The run finished successfully
refactor: migrate to Gitea server
2022-09-25 14:23:46 +02:00

33 lines
867 B
Go

package api
import (
"io"
"net/http"
"path"
"strings"
"time"
"code.icb4dc0.de/prskr/goveal/assets"
"code.icb4dc0.de/prskr/goveal/fs"
"code.icb4dc0.de/prskr/goveal/web"
)
func FileSystemMiddleware(fallthroughHandler http.Handler, wdfs fs.FS) http.Handler {
layers := []fs.FS{wdfs}
layers = append([]fs.FS{web.WebFS, assets.Assets}, layers...)
layeredFS := fs.Layered{Layers: layers}
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
reqPath := strings.TrimPrefix(req.URL.Path, "/")
if f, err := layeredFS.Open(reqPath); err != nil {
fallthroughHandler.ServeHTTP(writer, req)
return
} else if readSeeker, ok := f.(io.ReadSeeker); ok {
http.ServeContent(writer, req, path.Base(reqPath), time.Now(), readSeeker)
_ = f.Close()
} else {
_ = f.Close()
fallthroughHandler.ServeHTTP(writer, req)
}
})
}