41 lines
931 B
Go
41 lines
931 B
Go
|
package handlers
|
||
|
|
||
|
import (
|
||
|
"templ-test/client"
|
||
|
"templ-test/services"
|
||
|
|
||
|
"github.com/a-h/templ"
|
||
|
"github.com/gorilla/sessions"
|
||
|
"github.com/labstack/echo-contrib/session"
|
||
|
"github.com/labstack/echo/v4"
|
||
|
)
|
||
|
|
||
|
type Handlers struct {
|
||
|
Server *echo.Echo
|
||
|
api client.ApiClient
|
||
|
}
|
||
|
|
||
|
func NewHandlerClient(api client.ApiClient, cfg services.EnvConfig) *Handlers {
|
||
|
h := Handlers{
|
||
|
api: api,
|
||
|
}
|
||
|
|
||
|
e := echo.New()
|
||
|
e.Use(session.Middleware(sessions.NewCookieStore([]byte(cfg.CookieSecret))))
|
||
|
e.GET("/", h.HomeHandler)
|
||
|
e.GET("/list", h.ListHandler)
|
||
|
|
||
|
auth := e.Group("/auth")
|
||
|
auth.GET("/login", h.AuthLogin)
|
||
|
auth.POST("/login", h.AuthLoginPost)
|
||
|
|
||
|
h.Server = e
|
||
|
return &h
|
||
|
}
|
||
|
|
||
|
func Render(ctx echo.Context, statusCode int, t templ.Component) error {
|
||
|
ctx.Response().Writer.WriteHeader(statusCode)
|
||
|
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
|
||
|
return t.Render(ctx.Request().Context(), ctx.Response().Writer)
|
||
|
}
|