refactor: transitioned auth controller to use the new controller structure;
feat: setup DI for controllers; refactor: marked old utils and routes package parts as deprecated
This commit is contained in:
@@ -23,12 +23,12 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @Summary Change account password
|
||||
// @Tags Account
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /account/changePassword [put]
|
||||
// @Summary Change account password
|
||||
// @Tags Account
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /account/changePassword [put]
|
||||
func ChangePassword(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package controllers
|
||||
|
||||
import (
|
||||
errs "easywish/internal/errors"
|
||||
"easywish/internal/middleware"
|
||||
"easywish/internal/models"
|
||||
"easywish/internal/services"
|
||||
"easywish/internal/utils"
|
||||
@@ -31,249 +30,258 @@ import (
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AuthController interface {
|
||||
RegistrationBegin(c *gin.Context)
|
||||
RegistrationComplete(c *gin.Context)
|
||||
Login(c *gin.Context)
|
||||
Refresh(c *gin.Context)
|
||||
PasswordResetBegin(c *gin.Context)
|
||||
PasswordResetComplete(c *gin.Context)
|
||||
ChangePassword(c *gin.Context)
|
||||
Router
|
||||
}
|
||||
|
||||
type authControllerImpl struct {
|
||||
log *zap.Logger
|
||||
auth services.AuthService
|
||||
}
|
||||
|
||||
func NewAuthController(_log *zap.Logger, _auth services.AuthService) AuthController {
|
||||
return &authControllerImpl{log: _log, auth: _auth}
|
||||
}
|
||||
|
||||
// @Summary Acquire tokens via login credentials (and 2FA code if needed)
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.LoginRequest true " "
|
||||
// @Success 200 {object} models.LoginResponse " "
|
||||
// @Failure 403 "Invalid login credentials"
|
||||
// @Router /auth/login [post]
|
||||
func (a *authControllerImpl) Login(c *gin.Context) {
|
||||
request, ok := utils.GetRequest[models.LoginRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := a.auth.Login(request.User, request.Body)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// @Summary Request password reset email
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.PasswordResetBeginRequest true " "
|
||||
// @Router /auth/passwordResetBegin [post]
|
||||
// @Success 200 "Reset code sent to the email if it is attached to an account"
|
||||
// @Failure 429 "Too many recent requests for this email"
|
||||
func (a *authControllerImpl) PasswordResetBegin(c *gin.Context) {
|
||||
request, ok := utils.GetRequest[models.PasswordResetBeginRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := a.auth.PasswordResetBegin(request.Body)
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrTooManyRequests) {
|
||||
c.Status(http.StatusTooManyRequests)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// @Summary Complete password reset via email code
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.PasswordResetCompleteRequest true " "
|
||||
// @Router /auth/passwordResetComplete [post]
|
||||
// @Success 200 {object} models.PasswordResetCompleteResponse " "
|
||||
// @Success 403 "Wrong verification code or username"
|
||||
func (a *authControllerImpl) PasswordResetComplete(c *gin.Context) {
|
||||
|
||||
request, ok := utils.GetRequest[models.PasswordResetCompleteRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := a.auth.PasswordResetComplete(request.Body)
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
|
||||
// @Summary Receive new tokens via refresh token
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RefreshRequest true " "
|
||||
// @Router /auth/refresh [post]
|
||||
// @Success 200 {object} models.RefreshResponse " "
|
||||
// @Failure 401 "Invalid refresh token"
|
||||
func (a *authControllerImpl) Refresh(c *gin.Context) {
|
||||
|
||||
request, ok := utils.GetRequest[models.RefreshRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := a.auth.Refresh(request.Body)
|
||||
if err != nil {
|
||||
if utils.ErrorIsOneOf(
|
||||
err,
|
||||
errs.ErrTokenExpired,
|
||||
errs.ErrTokenInvalid,
|
||||
errs.ErrInvalidToken,
|
||||
errs.ErrWrongTokenType,
|
||||
errs.ErrSessionNotFound,
|
||||
errs.ErrSessionTerminated,
|
||||
) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// @Summary Register an account
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RegistrationBeginRequest true " "
|
||||
// @Success 200 "Account is created and awaiting verification"
|
||||
// @Failure 409 "Username or email is already taken"
|
||||
// @Failure 429 "Too many recent registration attempts for this email"
|
||||
// @Router /auth/registrationBegin [post]
|
||||
func (a *authControllerImpl) RegistrationBegin(c *gin.Context) {
|
||||
|
||||
request, ok := utils.GetRequest[models.RegistrationBeginRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err := a.auth.RegistrationBegin(request.Body)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrUsernameTaken) || errors.Is(err, errs.ErrEmailTaken) {
|
||||
c.Status(http.StatusConflict)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
// @Summary Confirm with code, finish creating the account
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RegistrationCompleteRequest true " "
|
||||
// @Success 200 {object} models.RegistrationCompleteResponse " "
|
||||
// @Failure 403 "Invalid email or verification code"
|
||||
// @Router /auth/registrationComplete [post]
|
||||
func (a *authControllerImpl) RegistrationComplete(c *gin.Context) {
|
||||
request, ok := utils.GetRequest[models.RegistrationCompleteRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := a.auth.RegistrationComplete(request.User, request.Body)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else if errors.Is(err, errs.ErrUnauthorized) {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
// @Summary Set new password using the old password
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Param request body models.ChangePasswordRequest true " "
|
||||
// @Success 200 "Password successfully changed"
|
||||
// @Failure 403 "Invalid old password"
|
||||
// @Router /auth/changePassword [post]
|
||||
func (a *authControllerImpl) ChangePassword(c *gin.Context) {
|
||||
request, ok := utils.GetRequest[models.ChangePasswordRequest](c)
|
||||
if !ok {
|
||||
c.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
response, err := a.auth.ChangePassword(request.Body, request.User)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (a *authControllerImpl) RegisterRoutes(group *gin.RouterGroup) {
|
||||
group.POST("/registrationBegin", middleware.RequestMiddleware[models.RegistrationBeginRequest](enums.GuestRole), a.RegistrationBegin)
|
||||
group.POST("/registrationComplete", middleware.RequestMiddleware[models.RegistrationCompleteRequest](enums.GuestRole), a.RegistrationComplete)
|
||||
group.POST("/login", middleware.RequestMiddleware[models.LoginRequest](enums.GuestRole), a.Login)
|
||||
group.POST("/refresh", middleware.RequestMiddleware[models.RefreshRequest](enums.GuestRole), a.Refresh)
|
||||
group.POST("/passwordResetBegin", middleware.RequestMiddleware[models.PasswordResetBeginRequest](enums.GuestRole), a.PasswordResetBegin)
|
||||
group.POST("/passwordResetComplete", middleware.RequestMiddleware[models.PasswordResetCompleteRequest](enums.GuestRole), a.PasswordResetComplete)
|
||||
group.POST("/changePassword", middleware.RequestMiddleware[models.ChangePasswordRequest](enums.UserRole), a.ChangePassword)
|
||||
func NewAuthController(log *zap.Logger, auth services.AuthService) Controller {
|
||||
return &controllerImpl{
|
||||
Path: "/auth",
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Methods: []ControllerMethod{
|
||||
|
||||
// @Summary Register an account
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RegistrationBeginRequest true " "
|
||||
// @Success 200 "Account is created and awaiting verification"
|
||||
// @Failure 409 "Username or email is already taken"
|
||||
// @Failure 429 "Too many recent registration attempts for this email"
|
||||
// @Router /auth/registrationBegin [post]
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/registrationBegin",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
request, err := GetRequest[models.RegistrationBeginRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = auth.RegistrationBegin(request.Body); if err != nil {
|
||||
if errors.Is(err, errs.ErrUsernameTaken) || errors.Is(err, errs.ErrEmailTaken) {
|
||||
c.Status(http.StatusConflict)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
return
|
||||
},
|
||||
},
|
||||
|
||||
// @Summary Confirm with code, finish creating the account
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RegistrationCompleteRequest true " "
|
||||
// @Success 200 {object} models.RegistrationCompleteResponse " "
|
||||
// @Failure 403 "Invalid email or verification code"
|
||||
// @Router /auth/registrationComplete [post]
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/registrationComplete",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
request, err := GetRequest[models.RegistrationCompleteRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := auth.RegistrationComplete(request.User, request.Body)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else if errors.Is(err, errs.ErrUnauthorized) {
|
||||
c.Status(http.StatusUnauthorized)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
},
|
||||
},
|
||||
|
||||
// @Summary Acquire tokens via login credentials (and 2FA code if needed)
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.LoginRequest true " "
|
||||
// @Success 200 {object} models.LoginResponse " "
|
||||
// @Failure 403 "Invalid login credentials"
|
||||
// @Router /auth/login [post]
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/login",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
request, err := GetRequest[models.LoginRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := auth.Login(request.User, request.Body)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
},
|
||||
},
|
||||
|
||||
// @Summary Receive new tokens via refresh token
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.RefreshRequest true " "
|
||||
// @Router /auth/refresh [post]
|
||||
// @Success 200 {object} models.RefreshResponse " "
|
||||
// @Failure 401 "Invalid refresh token"
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/refresh",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
request, err := GetRequest[models.RefreshRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := auth.Refresh(request.Body)
|
||||
if err != nil {
|
||||
if utils.ErrorIsOneOf(
|
||||
err,
|
||||
errs.ErrTokenExpired,
|
||||
errs.ErrTokenInvalid,
|
||||
errs.ErrInvalidToken,
|
||||
errs.ErrWrongTokenType,
|
||||
errs.ErrSessionNotFound,
|
||||
errs.ErrSessionTerminated,
|
||||
) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
},
|
||||
},
|
||||
|
||||
// @Summary Request password reset email
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.PasswordResetBeginRequest true " "
|
||||
// @Router /auth/passwordResetBegin [post]
|
||||
// @Success 200 "Reset code sent to the email if it is attached to an account"
|
||||
// @Failure 429 "Too many recent requests for this email"
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/passwordResetBegin",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
request, err := GetRequest[models.PasswordResetBeginRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := auth.PasswordResetBegin(request.Body)
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrTooManyRequests) {
|
||||
c.Status(http.StatusTooManyRequests)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
},
|
||||
},
|
||||
|
||||
|
||||
// @Summary Complete password reset via email code
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body models.PasswordResetCompleteRequest true " "
|
||||
// @Router /auth/passwordResetComplete [post]
|
||||
// @Success 200 {object} models.PasswordResetCompleteResponse " "
|
||||
// @Success 403 "Wrong verification code or username"
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/passwordResetComplete",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
request, err := GetRequest[models.PasswordResetCompleteRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := auth.PasswordResetComplete(request.Body)
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
},
|
||||
},
|
||||
|
||||
// @Summary Set new password using the old password
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Param request body models.ChangePasswordRequest true " "
|
||||
// @Success 200 "Password successfully changed"
|
||||
// @Failure 403 "Invalid old password"
|
||||
// @Router /auth/changePassword [post]
|
||||
{
|
||||
HttpMethod: POST,
|
||||
Path: "/changePassword",
|
||||
Authorization: enums.UserRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
request, err := GetRequest[models.ChangePasswordRequest](c); if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
response, err := auth.ChangePassword(request.Body, request.User)
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, errs.ErrForbidden) {
|
||||
c.Status(http.StatusForbidden)
|
||||
} else {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, response)
|
||||
},
|
||||
},
|
||||
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ package controllers
|
||||
|
||||
import (
|
||||
"easywish/internal/dto"
|
||||
"easywish/internal/middleware"
|
||||
"easywish/internal/services"
|
||||
"easywish/internal/utils/enums"
|
||||
"easywish/internal/validation"
|
||||
@@ -48,39 +47,16 @@ type ControllerMethod struct {
|
||||
|
||||
type controllerImpl struct {
|
||||
Path string
|
||||
Authorization enums.Role
|
||||
Middleware []gin.HandlerFunc
|
||||
Methods []ControllerMethod
|
||||
}
|
||||
|
||||
func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService) *gin.RouterGroup {
|
||||
type Controller interface {
|
||||
Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService)
|
||||
}
|
||||
|
||||
func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService) {
|
||||
ctrlGroup := group.Group(ctrl.Path)
|
||||
ctrlGroup.Use(middleware.AuthMiddleware(log, auth))
|
||||
ctrlGroup.Use(gin.HandlerFunc(func(c *gin.Context) {
|
||||
ip := c.ClientIP()
|
||||
userAgent := c.Request.UserAgent()
|
||||
sessionInfoFromCtx, ok := c.Get("session_info"); if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid or missing session data"})
|
||||
return
|
||||
}
|
||||
|
||||
sessionInfo := sessionInfoFromCtx.(dto.SessionInfo)
|
||||
|
||||
if sessionInfo.Role < ctrl.Authorization {
|
||||
c.AbortWithStatusJSON(
|
||||
http.StatusForbidden,
|
||||
gin.H{"error": "Insufficient authorization for this controller"})
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("client_info", dto.ClientInfo{
|
||||
SessionInfo: sessionInfo,
|
||||
IP: ip,
|
||||
UserAgent: userAgent,
|
||||
})
|
||||
|
||||
c.Next()
|
||||
}))
|
||||
ctrlGroup.Use(ctrl.Middleware...)
|
||||
|
||||
for _, method := range ctrl.Methods {
|
||||
@@ -100,11 +76,6 @@ func (ctrl *controllerImpl) Setup(group *gin.RouterGroup, log *zap.Logger, auth
|
||||
}),
|
||||
method.Function)...,
|
||||
)}
|
||||
return ctrlGroup
|
||||
}
|
||||
|
||||
type Controller interface {
|
||||
Setup(group *gin.RouterGroup, log *zap.Logger, auth services.AuthService)
|
||||
}
|
||||
|
||||
func GetRequest[ModelT any](c *gin.Context) (*dto.Request[ModelT], error) {
|
||||
|
||||
@@ -39,53 +39,53 @@ func NewProfileController() ProfileController {
|
||||
return &profileControllerImpl{}
|
||||
}
|
||||
|
||||
// @Summary Get someone's profile details
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param username path string true "Username"
|
||||
// @Security JWT
|
||||
// @Router /profile/{username} [get]
|
||||
// @Summary Get someone's profile details
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param username path string true "Username"
|
||||
// @Security JWT
|
||||
// @Router /profile/{username} [get]
|
||||
func (p *profileControllerImpl) GetProfile(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// @Summary Get own profile when authorized
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile/me [get]
|
||||
// @Summary Get own profile when authorized
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile/me [get]
|
||||
func (p *profileControllerImpl) GetOwnProfile(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// @Summary Update profile
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile [patch]
|
||||
// @Summary Update profile
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile [patch]
|
||||
func (p *profileControllerImpl) UpdateProfile(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// @Summary Get profile privacy settings
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile/privacy [get]
|
||||
// @Summary Get profile privacy settings
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile/privacy [get]
|
||||
func (p *profileControllerImpl) GetPrivacySettings(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
// @Summary Update profile privacy settings
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile/privacy [patch]
|
||||
// @Summary Update profile privacy settings
|
||||
// @Tags Profile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security JWT
|
||||
// @Router /profile/privacy [patch]
|
||||
func (p *profileControllerImpl) UpdatePrivacySettings(c *gin.Context) {
|
||||
c.Status(http.StatusNotImplemented)
|
||||
}
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"easywish/internal/models"
|
||||
"easywish/internal/utils/enums"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ServiceController interface {
|
||||
HealthCheck(c *gin.Context)
|
||||
Router
|
||||
}
|
||||
func NewServiceController() Controller {
|
||||
return &controllerImpl{
|
||||
Path: "/service",
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Methods: []ControllerMethod{
|
||||
|
||||
type serviceControllerImpl struct{}
|
||||
// Health godoc
|
||||
// @Summary Get health status
|
||||
// @Description Used internally for checking service health
|
||||
// @Tags Service
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} HealthStatus "Says whether it's healthy or not"
|
||||
// @Router /service/health [get]
|
||||
{
|
||||
HttpMethod: GET,
|
||||
Path: "/health",
|
||||
Authorization: enums.GuestRole,
|
||||
Middleware: []gin.HandlerFunc{},
|
||||
Function: func(c *gin.Context) {
|
||||
|
||||
func NewServiceController() ServiceController {
|
||||
return &serviceControllerImpl{}
|
||||
}
|
||||
c.JSON(http.StatusOK, models.HealthStatusResponse{Healthy: true,})
|
||||
|
||||
// HealthCheck implements ServiceController.
|
||||
// @Summary Get health status
|
||||
// @Description Used internally for checking service health
|
||||
// @Tags Service
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} HealthStatus "Says whether it's healthy or not"
|
||||
// @Router /service/health [get]
|
||||
func (s *serviceControllerImpl) HealthCheck(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"healthy": true})
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
// RegisterRoutes implements ServiceController.
|
||||
func (s *serviceControllerImpl) RegisterRoutes(group *gin.RouterGroup) {
|
||||
group.GET("/health", s.HealthCheck)
|
||||
}
|
||||
|
||||
type HealthStatus struct {
|
||||
Healthy bool `json:"healthy"`
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,72 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"easywish/internal/dto"
|
||||
"easywish/internal/middleware"
|
||||
"easywish/internal/services"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type SetupControllersParams struct {
|
||||
fx.In
|
||||
Controllers []Controller `group:"controllers"`
|
||||
Log *zap.Logger
|
||||
Auth services.AuthService
|
||||
Group *gin.Engine
|
||||
}
|
||||
|
||||
func setupControllers(p SetupControllersParams) {
|
||||
|
||||
apiGroup := p.Group.Group("/api")
|
||||
apiGroup.Use(middleware.AuthMiddleware(p.Log, p.Auth))
|
||||
apiGroup.Use(gin.HandlerFunc(func(c *gin.Context) {
|
||||
ip := c.ClientIP()
|
||||
userAgent := c.Request.UserAgent()
|
||||
sessionInfoFromCtx, ok := c.Get("session_info"); if !ok {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid or missing session data"})
|
||||
return
|
||||
}
|
||||
|
||||
sessionInfo := sessionInfoFromCtx.(dto.SessionInfo)
|
||||
|
||||
c.Set("client_info", dto.ClientInfo{
|
||||
SessionInfo: sessionInfo,
|
||||
IP: ip,
|
||||
UserAgent: userAgent,
|
||||
})
|
||||
|
||||
c.Next()
|
||||
}))
|
||||
for _, ctrl := range p.Controllers {
|
||||
ctrl.Setup(apiGroup, p.Log, p.Auth)
|
||||
}
|
||||
}
|
||||
|
||||
var Module = fx.Module("controllers",
|
||||
fx.Provide(
|
||||
NewServiceController,
|
||||
NewAuthController,
|
||||
NewProfileController,
|
||||
),
|
||||
fx.Annotate(NewAuthController, fx.ResultTags(`group:"controllers"`)),
|
||||
fx.Annotate(NewServiceController, fx.ResultTags(`group:"controllers"`)),
|
||||
),
|
||||
fx.Invoke(setupControllers),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user