Files
todo-en-go/file_server.go
2019-04-17 12:36:31 -04:00

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)
}))
}