go-cook-portal/handlers/handler.go

69 lines
1.3 KiB
Go
Raw Normal View History

2024-04-09 07:29:45 -07:00
package handlers
import (
"templ-test/client"
"templ-test/models"
2024-04-09 07:29:45 -07:00
"templ-test/services"
"github.com/a-h/templ"
2024-04-09 07:29:45 -07:00
"github.com/labstack/echo/v4"
)
const (
CookieToken = "token"
CookieRefreshToken = "refresh"
CookieUser = "user"
)
2024-04-09 07:29:45 -07:00
type Handlers struct {
api client.ApiClient
cfg services.EnvConfig
2024-04-09 07:29:45 -07:00
}
func NewHandlerClient(api client.ApiClient, cfg services.EnvConfig) *Handlers {
h := Handlers{
api: api,
cfg: cfg,
2024-04-09 07:29:45 -07:00
}
return &h
}
func (h *Handlers) Register(group echo.Group) {
group.GET("/", h.HomeHandler)
group.GET("/list", h.ListHandler)
auth := group.Group("/auth")
2024-04-09 07:29:45 -07:00
auth.GET("/login", h.AuthLogin)
auth.POST("/login", h.AuthLoginPost)
auth.GET("/cookie", h.AuthShowCookies)
2024-04-09 07:29:45 -07:00
}
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)
}
func GetCookieValues(ctx echo.Context) models.AllCookies {
m := models.AllCookies{}
token, err := ctx.Cookie(CookieToken)
if err == nil {
m.Token = token.Value
}
user, err := ctx.Cookie(CookieUser)
if err == nil {
m.Username = user.Value
}
refresh, err := ctx.Cookie(CookieRefreshToken)
if err == nil {
m.RefreshToken = refresh.Value
}
return m
}