fix add missing files
This commit is contained in:
@ -2,11 +2,33 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
const webBuildDir = "./web/build"
|
||||
|
||||
// spaHandler serves static files from dir and falls back to index.html for
|
||||
// any path that does not match an existing file on disk. This supports
|
||||
// SvelteKit (adapter-static, fallback: "index.html") running as a SPA.
|
||||
func spaHandler(dir string) http.HandlerFunc {
|
||||
fs := http.FileServer(http.Dir(dir))
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Resolve the requested path inside the build directory.
|
||||
abs := filepath.Join(dir, filepath.Clean("/"+r.URL.Path))
|
||||
if info, err := os.Stat(abs); err == nil && !info.IsDir() {
|
||||
// File exists — serve it normally (assets, icons, _app/*, …).
|
||||
fs.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
// No matching file found: serve the SPA entry point.
|
||||
http.ServeFile(w, r, filepath.Join(dir, "index.html"))
|
||||
}
|
||||
}
|
||||
|
||||
func NewRouter(h *Handler) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
@ -29,7 +51,7 @@ func NewRouter(h *Handler) http.Handler {
|
||||
})
|
||||
})
|
||||
|
||||
r.Handle("/*", http.FileServer(http.Dir("./web/dist")))
|
||||
r.Handle("/*", spaHandler(webBuildDir))
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user