Render a default route and static files from a function in lib
This commit is contained in:
@@ -147,3 +147,20 @@ func (h HtmxGetRoute) ToUrl(language Language, queryParams ...HtmxPathParam) str
|
|||||||
func (h HtmxGetRoute) GetHtmx(language Language, queryParams ...HtmxPathParam) gomponents.Node {
|
func (h HtmxGetRoute) GetHtmx(language Language, queryParams ...HtmxPathParam) gomponents.Node {
|
||||||
return htmx.Get(h.ToUrl(language, queryParams...))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user