Add language to routing as well
This commit is contained in:
+14
-10
@@ -12,16 +12,17 @@ type HtmxContext interface {
|
||||
}
|
||||
|
||||
type HtmxContextImpl struct {
|
||||
req *http.Request
|
||||
res http.ResponseWriter
|
||||
req *http.Request
|
||||
res http.ResponseWriter
|
||||
language Language
|
||||
}
|
||||
|
||||
func (c *HtmxContextImpl) GetLanguage() Language {
|
||||
return En
|
||||
return c.language
|
||||
}
|
||||
|
||||
func NewHtmxContext(req *http.Request, res http.ResponseWriter) HtmxContext {
|
||||
return &HtmxContextImpl{req: req, res: res}
|
||||
func NewHtmxContext(req *http.Request, res http.ResponseWriter, language Language) HtmxContext {
|
||||
return &HtmxContextImpl{req: req, res: res, language: language}
|
||||
}
|
||||
|
||||
func (c *HtmxContextImpl) Render(node gomponents.Node) {
|
||||
@@ -53,16 +54,19 @@ type HtmxGetRoute struct {
|
||||
Path string
|
||||
RouteFunc HtmxRouteFunc
|
||||
PathParams []HtmxPathParam
|
||||
Languages []Language
|
||||
}
|
||||
|
||||
func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc) *HtmxGetRoute {
|
||||
return &HtmxGetRoute{Path: path, RouteFunc: routeFunc}
|
||||
func NewHtmxGetRoute(path string, routeFunc HtmxRouteFunc, languages ...Language) *HtmxGetRoute {
|
||||
return &HtmxGetRoute{Path: path, RouteFunc: routeFunc, Languages: languages}
|
||||
}
|
||||
|
||||
func (h HtmxGetRoute) Add(server *http.ServeMux) {
|
||||
server.HandleFunc("GET "+h.Path, func(res http.ResponseWriter, req *http.Request) {
|
||||
h.RouteFunc(NewHtmxContext(req, res))
|
||||
})
|
||||
for _, language := range h.Languages {
|
||||
server.HandleFunc("GET /"+string(language)+h.Path, func(res http.ResponseWriter, req *http.Request) {
|
||||
h.RouteFunc(NewHtmxContext(req, res, language))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (h HtmxGetRoute) WithPathParams(params ...HtmxPathParam) HtmxRoute {
|
||||
|
||||
Reference in New Issue
Block a user