21 lines
427 B
Go
21 lines
427 B
Go
package main
|
|
|
|
import (
|
|
"github.com/go-chi/chi"
|
|
"net/http"
|
|
)
|
|
|
|
func fileServer(r chi.Router, path string, root http.FileSystem) {
|
|
fs := http.StripPrefix(path, http.FileServer(root))
|
|
|
|
if path != "/" && path[len(path)-1] != '/' {
|
|
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
|
|
path += "/"
|
|
}
|
|
path += "*"
|
|
|
|
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fs.ServeHTTP(w, r)
|
|
}))
|
|
}
|