2024-03-20 17:54:23 -07:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-03-31 17:48:23 -07:00
|
|
|
"net/http"
|
2024-03-20 17:54:23 -07:00
|
|
|
|
2024-04-20 08:09:24 -07:00
|
|
|
"git.jamestombleson.com/jtom38/go-cook/internal/domain"
|
|
|
|
"git.jamestombleson.com/jtom38/go-cook/internal/services"
|
2024-04-05 17:50:29 -07:00
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
echojwt "github.com/labstack/echo-jwt/v4"
|
2024-03-20 17:54:23 -07:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2024-03-31 17:48:23 -07:00
|
|
|
Config domain.EnvConfig
|
|
|
|
|
2024-04-21 08:59:51 -07:00
|
|
|
users services.UserService
|
|
|
|
recipes services.Recipes
|
|
|
|
refreshTokens services.RefreshToken
|
2024-03-20 17:54:23 -07:00
|
|
|
}
|
|
|
|
|
2024-03-31 17:48:23 -07:00
|
|
|
func NewHandler(conn *sql.DB, cfg domain.EnvConfig) *Handler {
|
2024-03-20 17:54:23 -07:00
|
|
|
return &Handler{
|
2024-04-21 08:59:51 -07:00
|
|
|
Config: cfg,
|
|
|
|
|
|
|
|
users: services.NewUserService(conn),
|
|
|
|
recipes: services.NewRecipesService(conn),
|
|
|
|
refreshTokens: services.NewRefreshTokenService(conn),
|
2024-03-20 17:54:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Register(v1 *echo.Group) {
|
2024-03-26 17:54:22 -07:00
|
|
|
jwtConfig := echojwt.Config{
|
|
|
|
NewClaimsFunc: func(c echo.Context) jwt.Claims {
|
|
|
|
return new(JwtToken)
|
|
|
|
},
|
2024-03-31 17:48:23 -07:00
|
|
|
SigningKey: []byte(h.Config.JwtSecret),
|
2024-03-26 17:54:22 -07:00
|
|
|
}
|
|
|
|
|
2024-04-03 16:12:18 -07:00
|
|
|
auth := v1.Group("/auth")
|
|
|
|
auth.POST("/login", h.AuthLogin)
|
|
|
|
auth.POST("/register", h.AuthRegister)
|
|
|
|
auth.Use(echojwt.WithConfig(jwtConfig))
|
2024-04-04 15:30:38 -07:00
|
|
|
auth.POST("/scopes/add", h.AddScopes)
|
|
|
|
auth.POST("/scopes/remove", h.RemoveScopes)
|
2024-04-21 08:59:51 -07:00
|
|
|
auth.POST("/refreshToken", h.RefreshJwtToken)
|
2024-04-05 17:50:29 -07:00
|
|
|
|
2024-04-03 16:12:18 -07:00
|
|
|
demo := v1.Group("/demo")
|
2024-03-26 17:54:22 -07:00
|
|
|
demo.GET("/hello", h.DemoHello)
|
2024-03-20 17:54:23 -07:00
|
|
|
demo.GET("/hello/:who", h.HelloWho)
|
2024-03-26 17:54:22 -07:00
|
|
|
|
|
|
|
demo.Use(echojwt.WithConfig(jwtConfig))
|
2024-03-23 08:26:49 -07:00
|
|
|
demo.GET("/hello/body", h.HelloBody)
|
2024-03-20 17:54:23 -07:00
|
|
|
|
2024-03-26 17:54:22 -07:00
|
|
|
protected := v1.Group("/demo/protected")
|
2024-03-27 21:55:25 -07:00
|
|
|
protected.Use(echojwt.WithConfig(jwtConfig))
|
|
|
|
protected.GET("", h.ProtectedRoute)
|
2024-03-26 17:54:22 -07:00
|
|
|
|
|
|
|
//recipes := v1.Group("/recipe")
|
|
|
|
|
|
|
|
//users := v1.Group("/users")
|
|
|
|
//users.POST("/register", h.RegisterUser)
|
|
|
|
//users.POST("/login", h.LoginUser)
|
|
|
|
//users.POST("/update/password", h.UpdatePassword)
|
2024-03-20 17:54:23 -07:00
|
|
|
}
|
2024-03-31 17:48:23 -07:00
|
|
|
|
|
|
|
func (h *Handler) ReturnUnauthorizedResponse(c echo.Context, message string) error {
|
|
|
|
return c.JSON(http.StatusUnauthorized, domain.ErrorResponse{
|
2024-04-03 16:12:18 -07:00
|
|
|
Success: false,
|
|
|
|
Message: message,
|
2024-03-31 17:48:23 -07:00
|
|
|
})
|
|
|
|
}
|
2024-03-31 18:05:33 -07:00
|
|
|
|
|
|
|
func (h *Handler) InternalServerErrorResponse(c echo.Context, message string) error {
|
|
|
|
return c.JSON(http.StatusServiceUnavailable, domain.ErrorResponse{
|
2024-04-03 16:12:18 -07:00
|
|
|
Success: false,
|
2024-03-31 18:05:33 -07:00
|
|
|
Message: message,
|
|
|
|
})
|
|
|
|
}
|