feat(m4): SvelteKit frontend - today, week, history, settings views
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
@@ -9,7 +10,14 @@ import (
|
||||
)
|
||||
|
||||
// NewRouter builds the full HTTP router.
|
||||
func NewRouter(authToken string, entrySvc *service.EntryService, daySvc *service.DayService, settingsSvc *service.SettingsService, weekSvc *service.WeekService) http.Handler {
|
||||
func NewRouter(
|
||||
authToken string,
|
||||
entrySvc *service.EntryService,
|
||||
daySvc *service.DayService,
|
||||
settingsSvc *service.SettingsService,
|
||||
weekSvc *service.WeekService,
|
||||
staticFiles fs.FS,
|
||||
) http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.RequestID)
|
||||
r.Use(middleware.RealIP)
|
||||
@@ -38,5 +46,23 @@ func NewRouter(authToken string, entrySvc *service.EntryService, daySvc *service
|
||||
weekH.Routes(r)
|
||||
})
|
||||
|
||||
// Serve embedded SPA if available (production build)
|
||||
if staticFiles != nil {
|
||||
fileServer := http.FileServer(http.FS(staticFiles))
|
||||
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Try to open the requested path; fall back to index.html for SPA routing
|
||||
path := r.URL.Path
|
||||
if path == "/" || path == "" {
|
||||
path = "index.html"
|
||||
} else {
|
||||
path = path[1:] // strip leading slash
|
||||
}
|
||||
if _, err := staticFiles.Open(path); err != nil {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user