package handlers import ( "context" "github.com/a-h/templ" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "git.jamestombleson.com/jtom38/newsbot-portal/apiclient" "git.jamestombleson.com/jtom38/newsbot-portal/internal/config" ) const ( ErrParameterIdMissing = "The requested parameter ID was not found." ErrParameterMissing = "The requested parameter was not found found:" ErrUnableToParseId = "Unable to parse the requested ID" ErrRecordMissing = "The requested record was not found" ErrFailedToCreateRecord = "The record was not created due to a database problem" ErrFailedToUpdateRecord = "The requested record was not updated due to a database problem" ErrUserUnknown = "User is unknown" ErrYouDontOwnTheRecord = "The record requested does not belong to you" ResponseMessageSuccess = "Success" ) var ( ErrIdValueMissing string = "id value is missing" ErrValueNotUuid string = "a value given was expected to be a uuid but was not correct." ErrNoRecordFound string = "no record was found." ErrUnableToConvertToJson string = "Unable to convert to json" ) type Handler struct { Router *echo.Echo config config.Configs api apiclient.ApiClient } func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient.ApiClient) *Handler { s := &Handler{ config: configs, api: apiClient, } //jwtConfig := echojwt.Config{ // NewClaimsFunc: func(c echo.Context) jwt.Claims { // return new(JwtToken) // }, // SigningKey: []byte(configs.JwtSecret), //} router := echo.New() router.Pre(middleware.RemoveTrailingSlash()) router.Pre(middleware.Logger()) router.Pre(middleware.Recover()) router.GET("/", s.HomeIndex) router.GET("/about", s.HomeAbout) debug := router.Group("/debug") debug.GET("/cookies", s.DebugCookies) articles := router.Group("/articles") articles.GET("", s.ArticlesList) users := router.Group("/users") users.GET("/login", s.UserLogin) users.POST("/login", s.UserAfterLogin) users.GET("/signup", s.UserSignUp) users.POST("/signup", s.UserAfterSignUp) s.Router = router 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. //func (s *Handler) ValidateJwtToken(c echo.Context, requiredScope string) (JwtToken, error) { // token, err := s.getJwtTokenFromContext(c) // if err != nil { // s.WriteMessage(c, ErrJwtMissing, http.StatusUnauthorized) // } // // err = token.hasExpired() // if err != nil { // return JwtToken{}, errors.New(ErrJwtExpired) // //s.WriteMessage(c, ErrJwtExpired, http.StatusUnauthorized) // } // // err = token.hasScope(requiredScope) // if err != nil { // return JwtToken{}, errors.New(ErrJwtScopeMissing) // //s.WriteMessage(c, ErrJwtScopeMissing, http.StatusUnauthorized) // } // // if token.Iss != s.config.ServerAddress { // return JwtToken{}, errors.New(ErrJwtInvalidIssuer) // //s.WriteMessage(c, ErrJwtInvalidIssuer, http.StatusUnauthorized) // } // // return token, nil //} //func (s *Handler) GetUserIdFromJwtToken(c echo.Context) int64 { // token, err := s.getJwtTokenFromContext(c) // if err != nil { // s.WriteMessage(c, ErrJwtMissing, http.StatusUnauthorized) // } // // return token.GetUserId() //}