Render a default route and static files from a function in lib

This commit is contained in:
Achim Rohn
2025-08-13 11:42:36 +02:00
parent 7cf7cb52c9
commit ff5c4711b0
+17
View File
@@ -147,3 +147,20 @@ func (h HtmxGetRoute) ToUrl(language Language, queryParams ...HtmxPathParam) str
func (h HtmxGetRoute) GetHtmx(language Language, queryParams ...HtmxPathParam) gomponents.Node {
return htmx.Get(h.ToUrl(language, queryParams...))
}
func HandleStaticAndDefaultPath(server *http.ServeMux, defaultRoute HtmxRoute, defaultLanguage Language) func(http.ResponseWriter, *http.Request) {
staticHandler := http.StripPrefix("/static", http.FileServer(http.Dir("./static")))
server.Handle("GET /static", staticHandler)
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if strings.HasPrefix(path, "/static") {
staticHandler.ServeHTTP(w, r)
return
}
if path == "/" {
defaultRoute.Execute(NewHtmxContext(r, w, defaultLanguage))
return
}
http.Error(w, "Not found", http.StatusNotFound)
}
}