moved the Render func to util given I will add new context items

This commit is contained in:
James Tombleson 2024-05-19 10:02:08 -07:00
parent 2810aa9de0
commit 97c08947a9
2 changed files with 16 additions and 34 deletions

View File

@ -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.

View File

@ -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)
}