81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package v1
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"git.jamestombleson.com/jtom38/go-cook/internal/domain"
|
|
"git.jamestombleson.com/jtom38/go-cook/internal/services"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
echojwt "github.com/labstack/echo-jwt/v4"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
type Handler struct {
|
|
Config domain.EnvConfig
|
|
|
|
users services.UserService
|
|
recipes services.Recipes
|
|
refreshTokens services.RefreshToken
|
|
}
|
|
|
|
func NewHandler(conn *sql.DB, cfg domain.EnvConfig) *Handler {
|
|
return &Handler{
|
|
Config: cfg,
|
|
|
|
users: services.NewUserService(conn),
|
|
recipes: services.NewRecipesService(conn),
|
|
refreshTokens: services.NewRefreshTokenService(conn),
|
|
}
|
|
}
|
|
|
|
func (h *Handler) Register(v1 *echo.Group) {
|
|
jwtConfig := echojwt.Config{
|
|
NewClaimsFunc: func(c echo.Context) jwt.Claims {
|
|
return new(JwtToken)
|
|
},
|
|
SigningKey: []byte(h.Config.JwtSecret),
|
|
}
|
|
|
|
auth := v1.Group("/auth")
|
|
auth.POST("/login", h.AuthLogin)
|
|
auth.POST("/register", h.AuthRegister)
|
|
auth.Use(echojwt.WithConfig(jwtConfig))
|
|
auth.POST("/scopes/add", h.AddScopes)
|
|
auth.POST("/scopes/remove", h.RemoveScopes)
|
|
auth.POST("/refreshToken", h.RefreshJwtToken)
|
|
|
|
demo := v1.Group("/demo")
|
|
demo.GET("/hello", h.DemoHello)
|
|
demo.GET("/hello/:who", h.HelloWho)
|
|
|
|
demo.Use(echojwt.WithConfig(jwtConfig))
|
|
demo.GET("/hello/body", h.HelloBody)
|
|
|
|
protected := v1.Group("/demo/protected")
|
|
protected.Use(echojwt.WithConfig(jwtConfig))
|
|
protected.GET("", h.ProtectedRoute)
|
|
|
|
//recipes := v1.Group("/recipe")
|
|
|
|
//users := v1.Group("/users")
|
|
//users.POST("/register", h.RegisterUser)
|
|
//users.POST("/login", h.LoginUser)
|
|
//users.POST("/update/password", h.UpdatePassword)
|
|
}
|
|
|
|
func (h *Handler) ReturnUnauthorizedResponse(c echo.Context, message string) error {
|
|
return c.JSON(http.StatusUnauthorized, domain.ErrorResponse{
|
|
Success: false,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
func (h *Handler) InternalServerErrorResponse(c echo.Context, message string) error {
|
|
return c.JSON(http.StatusServiceUnavailable, domain.ErrorResponse{
|
|
Success: false,
|
|
Message: message,
|
|
})
|
|
}
|