31 lines
863 B
Go
31 lines
863 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// spaHandler serves the built frontend from dir, falling back to index.html
|
|
// for any path that isn't an actual file so client-side routes (e.g. a list's
|
|
// /lion-fancy-sharpness URL) resolve correctly on a hard refresh.
|
|
func spaHandler(dir string) http.Handler {
|
|
fs := http.FileServer(http.Dir(dir))
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
cleanPath := filepath.Clean(r.URL.Path)
|
|
|
|
if strings.HasPrefix(cleanPath, "/assets/") {
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
|
} else {
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
}
|
|
|
|
if info, err := os.Stat(filepath.Join(dir, cleanPath)); err == nil && !info.IsDir() {
|
|
fs.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
http.ServeFile(w, r, filepath.Join(dir, "index.html"))
|
|
})
|
|
}
|