From 97c08947a9793ff5c2193d9205d39b956b7cf74e Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 19 May 2024 10:02:08 -0700 Subject: [PATCH] moved the Render func to util given I will add new context items --- internal/handlers/handler.go | 34 ---------------------------------- internal/handlers/util.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 34 deletions(-) diff --git a/internal/handlers/handler.go b/internal/handlers/handler.go index d7a482c..946979a 100644 --- a/internal/handlers/handler.go +++ b/internal/handlers/handler.go @@ -3,7 +3,6 @@ package handlers import ( "context" - "github.com/a-h/templ" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" @@ -76,39 +75,6 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient. return s } -// This custom Render replaces Echo's echo.Context.Render() with templ's templ.Component.Render(). -func Render(ctx echo.Context, statusCode int, t templ.Component) error { - ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML) - ctx.Response().Writer.WriteHeader(statusCode) - return t.Render(ctx.Request().Context(), ctx.Response().Writer) -} - -/* -func (s *Handler) WriteError(c echo.Context, errMessage error, HttpStatusCode int) error { - return c.JSON(HttpStatusCode, domain.BaseResponse{ - Message: errMessage.Error(), - }) -} - -func (s *Handler) WriteMessage(c echo.Context, msg string, HttpStatusCode int) error { - return c.JSON(HttpStatusCode, domain.BaseResponse{ - Message: msg, - }) -} - -func (s *Handler) InternalServerErrorResponse(c echo.Context, msg string) error { - return c.JSON(http.StatusInternalServerError, domain.BaseResponse{ - Message: msg, - }) -} - -func (s *Handler) UnauthorizedResponse(c echo.Context, msg string) error { - return c.JSON(http.StatusUnauthorized, domain.BaseResponse{ - Message: msg, - }) -} -*/ - // If the token is not valid then an json error will be returned. // If the token has the wrong scope, a json error will be returned. // If the token passes all the checks, it is valid and is returned back to the caller. diff --git a/internal/handlers/util.go b/internal/handlers/util.go index f4d1281..e619ef7 100644 --- a/internal/handlers/util.go +++ b/internal/handlers/util.go @@ -6,6 +6,7 @@ import ( "time" "git.jamestombleson.com/jtom38/newsbot-portal/internal/domain" + "github.com/a-h/templ" "github.com/golang-jwt/jwt/v4" "github.com/labstack/echo/v4" ) @@ -60,3 +61,18 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error return *claims, nil } + +func Render(ctx echo.Context, statusCode int, t templ.Component) error { + ctx.Response().Writer.WriteHeader(statusCode) + ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML) + + // take the request context and make it a var + request := ctx.Request().Context() + //Check to see if we the echo context has the cookie we are looking for, if so, create a new context based on what we had and add the value + //darkMode, err := ctx.Cookie(domain.CookieSettingsDarkMode) + //if err == nil { + // request = context.WithValue(request, domain.CookieSettingsDarkMode, darkMode.Value) + //} + + return t.Render(request, ctx.Response().Writer) +}