| 12345678910111213141516171819202122232425262728293031323334 |
- package web
- import (
- "embed"
- "io/fs"
- "net/http"
- "strings"
- )
- //go:embed all:html
- var htmlFS embed.FS
- func Handler() http.HandlerFunc {
- fsys, err := fs.Sub(htmlFS, "html")
- if err != nil {
- panic("failed to create sub filesystem: " + err.Error())
- }
- fileServer := http.FileServer(http.FS(fsys))
- return func(w http.ResponseWriter, r *http.Request) {
- path := r.URL.Path
- if path != "/" {
- cleanPath := strings.TrimPrefix(path, "/")
- if _, err := fs.Stat(fsys, cleanPath); err == nil {
- fileServer.ServeHTTP(w, r)
- return
- }
- }
- r.URL.Path = "/"
- fileServer.ServeHTTP(w, r)
- }
- }
|