godash/server/static.go
2022-10-20 22:23:47 +02:00

34 lines
891 B
Go

package server
import (
"github.com/go-chi/chi/v5"
"net/http"
"os"
"path/filepath"
"strings"
)
func (server *Server) serveStatic(folder string) {
workDir, _ := os.Getwd()
fileServer(server.Router, "/"+folder, http.Dir(filepath.Join(workDir, folder)))
}
func fileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit any URL parameters.")
}
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
rctx := chi.RouteContext(r.Context())
pathPrefix := strings.TrimSuffix(rctx.RoutePattern(), "/*")
fs := http.StripPrefix(pathPrefix, http.FileServer(root))
fs.ServeHTTP(w, r)
})
}