Remove fiber ctx from render

This commit is contained in:
Eden Kirin
2024-02-06 20:36:27 +01:00
parent 320fb3c4d3
commit a47e8a39b6
5 changed files with 10 additions and 12 deletions

View File

@ -1,16 +1,19 @@
package handlers package handlers
import ( import (
"fiber-sessions/app/router/requests"
"github.com/a-h/templ" "github.com/a-h/templ"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor" "github.com/gofiber/fiber/v2/middleware/adaptor"
) )
type RequestHandler = func(request *requests.Request) error
// example taken from https://github.com/a-h/templ/tree/main/examples/integration-gofiber // example taken from https://github.com/a-h/templ/tree/main/examples/integration-gofiber
func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error { func Render(request *requests.Request, component templ.Component, options ...func(*templ.ComponentHandler)) error {
componentHandler := templ.Handler(component) componentHandler := templ.Handler(component)
for _, o := range options { for _, o := range options {
o(componentHandler) o(componentHandler)
} }
return adaptor.HTTPHandler(componentHandler)(c) return adaptor.HTTPHandler(componentHandler)(request.Ctx)
} }

View File

@ -10,6 +10,6 @@ func Home(request *requests.Request) error {
Title: "Welcome to the demo", Title: "Welcome to the demo",
ActivePage: "home", ActivePage: "home",
} }
Render(request.Ctx, templates.Home(request, pc)) Render(request, templates.Home(request, pc))
return nil return nil
} }

View File

@ -11,7 +11,7 @@ var pcLogin templates.PageContext = templates.PageContext{
} }
func Login(request *requests.Request) error { func Login(request *requests.Request) error {
Render(request.Ctx, templates.RenderLogin(request, pcLogin)) Render(request, templates.RenderLogin(request, pcLogin))
return nil return nil
} }
@ -42,6 +42,6 @@ func LoginSubmit(request *requests.Request) error {
content.LoginError = "Invalid username or password" content.LoginError = "Invalid username or password"
} }
Render(request.Ctx, templates.LoginFormContent(content)) Render(request, templates.LoginFormContent(content))
return nil return nil
} }

View File

@ -10,6 +10,6 @@ func ProtectedPage(request *requests.Request) error {
Title: "Protected page", Title: "Protected page",
ActivePage: "protected-page", ActivePage: "protected-page",
} }
Render(request.Ctx, templates.ProtectedPage(request, pc)) Render(request, templates.ProtectedPage(request, pc))
return nil return nil
} }

View File

@ -1,5 +0,0 @@
package handlers
import "fiber-sessions/app/router/requests"
type RequestHandler = func(request *requests.Request) error