-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfs.go
More file actions
34 lines (29 loc) · 741 Bytes
/
fs.go
File metadata and controls
34 lines (29 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/rakyll/statik/fs"
)
// FileServer conveniently sets up a http.FileServer handler to serve
// static files from a http.FileSystem.
func fileServerHandler(r chi.Router) {
statikFS, err := fs.New()
if err != nil {
log.Fatal(err)
}
staticHandler := http.FileServer(statikFS)
path := "/"
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) {
staticHandler.ServeHTTP(w, r)
}))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = "/"
staticHandler.ServeHTTP(w, r)
})
}