refactor-controllers #5
@@ -15,17 +15,17 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
// @title Easywish client API
|
// @title Easywish client API
|
||||||
// @version 1.0
|
// @version 1.0
|
||||||
// @description Easy and feature-rich wishlist.
|
// @description Easy and feature-rich wishlist.
|
||||||
// @license.name GPL-3.0
|
// @license.name GPL-3.0
|
||||||
|
|
||||||
// @BasePath /api/
|
// @BasePath /api/
|
||||||
// @Schemes http
|
// @Schemes http
|
||||||
|
|
||||||
// @securityDefinitions.apikey JWT
|
// @securityDefinitions.apikey JWT
|
||||||
// @in header
|
// @in header
|
||||||
// @name Authorization
|
// @name Authorization
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@@ -46,7 +46,6 @@ import (
|
|||||||
"easywish/internal/database"
|
"easywish/internal/database"
|
||||||
"easywish/internal/logger"
|
"easywish/internal/logger"
|
||||||
redisclient "easywish/internal/redisClient"
|
redisclient "easywish/internal/redisClient"
|
||||||
"easywish/internal/routes"
|
|
||||||
"easywish/internal/services"
|
"easywish/internal/services"
|
||||||
"easywish/internal/validation"
|
"easywish/internal/validation"
|
||||||
|
|
||||||
@@ -74,7 +73,6 @@ func main() {
|
|||||||
validation.Module,
|
validation.Module,
|
||||||
|
|
||||||
controllers.Module,
|
controllers.Module,
|
||||||
routes.Module,
|
|
||||||
|
|
||||||
fx.Invoke(func(lc fx.Lifecycle, router *gin.Engine, syncLogger *logger.SyncLogger) {
|
fx.Invoke(func(lc fx.Lifecycle, router *gin.Engine, syncLogger *logger.SyncLogger) {
|
||||||
|
|
||||||
|
|||||||
@@ -413,7 +413,7 @@ const docTemplate = `{
|
|||||||
"200": {
|
"200": {
|
||||||
"description": "Says whether it's healthy or not",
|
"description": "Says whether it's healthy or not",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/controllers.HealthStatus"
|
"$ref": "#/definitions/models.HealthStatusResponse"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -421,14 +421,6 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
"controllers.HealthStatus": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"healthy": {
|
|
||||||
"type": "boolean"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.ChangePasswordRequest": {
|
"models.ChangePasswordRequest": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
@@ -447,6 +439,14 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"models.HealthStatusResponse": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"healthy": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"models.LoginRequest": {
|
"models.LoginRequest": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
@@ -530,7 +530,8 @@ const docTemplate = `{
|
|||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"refresh_token": {
|
"refresh_token": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"maxLength": 2000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -409,7 +409,7 @@
|
|||||||
"200": {
|
"200": {
|
||||||
"description": "Says whether it's healthy or not",
|
"description": "Says whether it's healthy or not",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/controllers.HealthStatus"
|
"$ref": "#/definitions/models.HealthStatusResponse"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -417,14 +417,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
"controllers.HealthStatus": {
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"healthy": {
|
|
||||||
"type": "boolean"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"models.ChangePasswordRequest": {
|
"models.ChangePasswordRequest": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
@@ -443,6 +435,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"models.HealthStatusResponse": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"healthy": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"models.LoginRequest": {
|
"models.LoginRequest": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
@@ -526,7 +526,8 @@
|
|||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
"refresh_token": {
|
"refresh_token": {
|
||||||
"type": "string"
|
"type": "string",
|
||||||
|
"maxLength": 2000
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
basePath: /api/
|
basePath: /api/
|
||||||
definitions:
|
definitions:
|
||||||
controllers.HealthStatus:
|
|
||||||
properties:
|
|
||||||
healthy:
|
|
||||||
type: boolean
|
|
||||||
type: object
|
|
||||||
models.ChangePasswordRequest:
|
models.ChangePasswordRequest:
|
||||||
properties:
|
properties:
|
||||||
old_password:
|
old_password:
|
||||||
@@ -17,6 +12,11 @@ definitions:
|
|||||||
- old_password
|
- old_password
|
||||||
- password
|
- password
|
||||||
type: object
|
type: object
|
||||||
|
models.HealthStatusResponse:
|
||||||
|
properties:
|
||||||
|
healthy:
|
||||||
|
type: boolean
|
||||||
|
type: object
|
||||||
models.LoginRequest:
|
models.LoginRequest:
|
||||||
properties:
|
properties:
|
||||||
password:
|
password:
|
||||||
@@ -71,6 +71,7 @@ definitions:
|
|||||||
models.RefreshRequest:
|
models.RefreshRequest:
|
||||||
properties:
|
properties:
|
||||||
refresh_token:
|
refresh_token:
|
||||||
|
maxLength: 2000
|
||||||
type: string
|
type: string
|
||||||
required:
|
required:
|
||||||
- refresh_token
|
- refresh_token
|
||||||
@@ -372,7 +373,7 @@ paths:
|
|||||||
"200":
|
"200":
|
||||||
description: Says whether it's healthy or not
|
description: Says whether it's healthy or not
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/controllers.HealthStatus'
|
$ref: '#/definitions/models.HealthStatusResponse'
|
||||||
summary: Get health status
|
summary: Get health status
|
||||||
tags:
|
tags:
|
||||||
- Service
|
- Service
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
errs "easywish/internal/errors"
|
errs "easywish/internal/errors"
|
||||||
"easywish/internal/middleware"
|
|
||||||
"easywish/internal/models"
|
"easywish/internal/models"
|
||||||
"easywish/internal/services"
|
"easywish/internal/services"
|
||||||
"easywish/internal/utils"
|
"easywish/internal/utils"
|
||||||
@@ -31,73 +30,91 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AuthController interface {
|
type AuthController struct {
|
||||||
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
|
auth services.AuthService
|
||||||
|
log *zap.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAuthController(_log *zap.Logger, _auth services.AuthService) AuthController {
|
func NewAuthController(log *zap.Logger, auth services.AuthService) Controller {
|
||||||
return &authControllerImpl{log: _log, auth: _auth}
|
ctrl := &AuthController{auth: auth, log: log}
|
||||||
}
|
|
||||||
|
|
||||||
// @Summary Acquire tokens via login credentials (and 2FA code if needed)
|
return &controllerImpl{
|
||||||
// @Tags Auth
|
Path: "/auth",
|
||||||
// @Accept json
|
Middleware: []gin.HandlerFunc{},
|
||||||
// @Produce json
|
Methods: []ControllerMethod{
|
||||||
// @Param request body models.LoginRequest true " "
|
{
|
||||||
// @Success 200 {object} models.LoginResponse " "
|
HttpMethod: POST,
|
||||||
// @Failure 403 "Invalid login credentials"
|
Path: "/registrationBegin",
|
||||||
// @Router /auth/login [post]
|
Authorization: enums.GuestRole,
|
||||||
func (a *authControllerImpl) Login(c *gin.Context) {
|
Middleware: []gin.HandlerFunc{},
|
||||||
request, ok := utils.GetRequest[models.LoginRequest](c)
|
Function: ctrl.registrationBeginHandler,
|
||||||
if !ok {
|
},
|
||||||
c.Status(http.StatusBadRequest)
|
{
|
||||||
return
|
HttpMethod: POST,
|
||||||
|
Path: "/registrationComplete",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.registrationCompleteHandler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
HttpMethod: POST,
|
||||||
|
Path: "/login",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.loginHandler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
HttpMethod: POST,
|
||||||
|
Path: "/refresh",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.refreshHandler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
HttpMethod: POST,
|
||||||
|
Path: "/passwordResetBegin",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.passwordResetBeginHandler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
HttpMethod: POST,
|
||||||
|
Path: "/passwordResetComplete",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.passwordResetCompleteHandler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
HttpMethod: POST,
|
||||||
|
Path: "/changePassword",
|
||||||
|
Authorization: enums.UserRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.changePasswordHandler,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
response, err := a.auth.Login(request.User, request.Body)
|
// @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 (ctrl *AuthController) registrationBeginHandler(c *gin.Context) {
|
||||||
|
request, err := GetRequest[models.RegistrationBeginRequest](c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
|
||||||
c.Status(http.StatusForbidden)
|
|
||||||
} else {
|
|
||||||
c.Status(http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, response)
|
_, err = ctrl.auth.RegistrationBegin(request.Body)
|
||||||
}
|
|
||||||
|
|
||||||
// @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 err != nil {
|
||||||
if errors.Is(err, errs.ErrTooManyRequests) {
|
if errors.Is(err, errs.ErrUsernameTaken) || errors.Is(err, errs.ErrEmailTaken) {
|
||||||
|
c.Status(http.StatusConflict)
|
||||||
|
} else if errors.Is(err, errs.ErrTooManyRequests) {
|
||||||
c.Status(http.StatusTooManyRequests)
|
c.Status(http.StatusTooManyRequests)
|
||||||
} else {
|
} else {
|
||||||
c.Status(http.StatusInternalServerError)
|
c.Status(http.StatusInternalServerError)
|
||||||
@@ -105,125 +122,24 @@ func (a *authControllerImpl) PasswordResetBegin(c *gin.Context) {
|
|||||||
return
|
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)
|
c.Status(http.StatusOK)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Confirm with code, finish creating the account
|
// @Summary Confirm with code, finish creating the account
|
||||||
// @Tags Auth
|
// @Tags Auth
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Param request body models.RegistrationCompleteRequest true " "
|
// @Param request body models.RegistrationCompleteRequest true " "
|
||||||
// @Success 200 {object} models.RegistrationCompleteResponse " "
|
// @Success 200 {object} models.RegistrationCompleteResponse " "
|
||||||
// @Failure 403 "Invalid email or verification code"
|
// @Failure 403 "Invalid email or verification code"
|
||||||
// @Router /auth/registrationComplete [post]
|
// @Router /auth/registrationComplete [post]
|
||||||
func (a *authControllerImpl) RegistrationComplete(c *gin.Context) {
|
func (ctrl *AuthController) registrationCompleteHandler(c *gin.Context) {
|
||||||
request, ok := utils.GetRequest[models.RegistrationCompleteRequest](c)
|
request, err := GetRequest[models.RegistrationCompleteRequest](c)
|
||||||
if !ok {
|
if err != nil {
|
||||||
c.Status(http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := a.auth.RegistrationComplete(request.User, request.Body)
|
response, err := ctrl.auth.RegistrationComplete(request.User, request.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
if errors.Is(err, errs.ErrForbidden) {
|
||||||
c.Status(http.StatusForbidden)
|
c.Status(http.StatusForbidden)
|
||||||
@@ -238,24 +154,21 @@ func (a *authControllerImpl) RegistrationComplete(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, response)
|
c.JSON(http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Set new password using the old password
|
// @Summary Acquire tokens via login credentials (and 2FA code if needed)
|
||||||
// @Tags Auth
|
// @Tags Auth
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Security JWT
|
// @Param request body models.LoginRequest true " "
|
||||||
// @Param request body models.ChangePasswordRequest true " "
|
// @Success 200 {object} models.LoginResponse " "
|
||||||
// @Success 200 "Password successfully changed"
|
// @Failure 403 "Invalid login credentials"
|
||||||
// @Failure 403 "Invalid old password"
|
// @Router /auth/login [post]
|
||||||
// @Router /auth/changePassword [post]
|
func (ctrl *AuthController) loginHandler(c *gin.Context) {
|
||||||
func (a *authControllerImpl) ChangePassword(c *gin.Context) {
|
request, err := GetRequest[models.LoginRequest](c)
|
||||||
request, ok := utils.GetRequest[models.ChangePasswordRequest](c)
|
if err != nil {
|
||||||
if !ok {
|
|
||||||
c.Status(http.StatusBadRequest)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := a.auth.ChangePassword(request.Body, request.User)
|
response, err := ctrl.auth.Login(request.User, request.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, errs.ErrForbidden) {
|
if errors.Is(err, errs.ErrForbidden) {
|
||||||
c.Status(http.StatusForbidden)
|
c.Status(http.StatusForbidden)
|
||||||
@@ -268,12 +181,119 @@ func (a *authControllerImpl) ChangePassword(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, response)
|
c.JSON(http.StatusOK, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *authControllerImpl) RegisterRoutes(group *gin.RouterGroup) {
|
// @Summary Receive new tokens via refresh token
|
||||||
group.POST("/registrationBegin", middleware.RequestMiddleware[models.RegistrationBeginRequest](enums.GuestRole), a.RegistrationBegin)
|
// @Tags Auth
|
||||||
group.POST("/registrationComplete", middleware.RequestMiddleware[models.RegistrationCompleteRequest](enums.GuestRole), a.RegistrationComplete)
|
// @Accept json
|
||||||
group.POST("/login", middleware.RequestMiddleware[models.LoginRequest](enums.GuestRole), a.Login)
|
// @Produce json
|
||||||
group.POST("/refresh", middleware.RequestMiddleware[models.RefreshRequest](enums.GuestRole), a.Refresh)
|
// @Param request body models.RefreshRequest true " "
|
||||||
group.POST("/passwordResetBegin", middleware.RequestMiddleware[models.PasswordResetBeginRequest](enums.GuestRole), a.PasswordResetBegin)
|
// @Router /auth/refresh [post]
|
||||||
group.POST("/passwordResetComplete", middleware.RequestMiddleware[models.PasswordResetCompleteRequest](enums.GuestRole), a.PasswordResetComplete)
|
// @Success 200 {object} models.RefreshResponse " "
|
||||||
group.POST("/changePassword", middleware.RequestMiddleware[models.ChangePasswordRequest](enums.UserRole), a.ChangePassword)
|
// @Failure 401 "Invalid refresh token"
|
||||||
|
func (ctrl *AuthController) refreshHandler(c *gin.Context) {
|
||||||
|
request, err := GetRequest[models.RefreshRequest](c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := ctrl.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, gin.H{"error": 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"
|
||||||
|
func (ctrl *AuthController) passwordResetBeginHandler(c *gin.Context) {
|
||||||
|
request, err := GetRequest[models.PasswordResetBeginRequest](c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ctrl.auth.PasswordResetBegin(request.Body)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, errs.ErrTooManyRequests) {
|
||||||
|
c.Status(http.StatusTooManyRequests)
|
||||||
|
} else {
|
||||||
|
c.Status(http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Status(http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @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 (ctrl *AuthController) passwordResetCompleteHandler(c *gin.Context) {
|
||||||
|
request, err := GetRequest[models.PasswordResetCompleteRequest](c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := ctrl.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]
|
||||||
|
func (ctrl *AuthController) changePasswordHandler(c *gin.Context) {
|
||||||
|
request, err := GetRequest[models.ChangePasswordRequest](c)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ctrl.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.Status(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|||||||
112
backend/internal/controllers/controller.go
Normal file
112
backend/internal/controllers/controller.go
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
// 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/services"
|
||||||
|
"easywish/internal/utils/enums"
|
||||||
|
"easywish/internal/validation"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/go-playground/validator/v10"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
GET = "GET"
|
||||||
|
POST = "POST"
|
||||||
|
PUT = "PUT"
|
||||||
|
PATCH = "PATCH"
|
||||||
|
DELETE = "DELETE"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ControllerMethod struct {
|
||||||
|
HttpMethod string
|
||||||
|
Path string
|
||||||
|
Authorization enums.Role
|
||||||
|
Middleware []gin.HandlerFunc
|
||||||
|
Function func (c *gin.Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
type controllerImpl struct {
|
||||||
|
Path string
|
||||||
|
Middleware []gin.HandlerFunc
|
||||||
|
Methods []ControllerMethod
|
||||||
|
}
|
||||||
|
|
||||||
|
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(ctrl.Middleware...)
|
||||||
|
|
||||||
|
for _, method := range ctrl.Methods {
|
||||||
|
ctrlGroup.Handle(
|
||||||
|
method.HttpMethod,
|
||||||
|
method.Path,
|
||||||
|
append(
|
||||||
|
method.Middleware,
|
||||||
|
gin.HandlerFunc(func(c *gin.Context) {
|
||||||
|
clientInfo, _ := c.Get("client_info")
|
||||||
|
if clientInfo.(dto.ClientInfo).Role < method.Authorization {
|
||||||
|
c.AbortWithStatusJSON(
|
||||||
|
http.StatusForbidden,
|
||||||
|
gin.H{"error": "Insufficient authorization for this method"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
method.Function)...,
|
||||||
|
)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRequest[ModelT any](c *gin.Context) (*dto.Request[ModelT], error) {
|
||||||
|
|
||||||
|
var body ModelT
|
||||||
|
if err := c.ShouldBindJSON(&body); err != nil {
|
||||||
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Think hard on a singleton for better performance
|
||||||
|
validate := validation.NewValidator()
|
||||||
|
|
||||||
|
if err := validate.Struct(body); err != nil {
|
||||||
|
errorList := err.(validator.ValidationErrors)
|
||||||
|
c.AbortWithStatusJSON(
|
||||||
|
http.StatusBadRequest,
|
||||||
|
gin.H{"error": errorList})
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cinfoFromCtx, ok := c.Get("client_info"); if !ok {
|
||||||
|
c.AbortWithStatusJSON(
|
||||||
|
http.StatusInternalServerError,
|
||||||
|
gin.H{"error": "Client info was not found"})
|
||||||
|
panic("No client_info found in gin context. Does the handler use AuthMiddleware?")
|
||||||
|
}
|
||||||
|
cinfo := cinfoFromCtx.(dto.ClientInfo)
|
||||||
|
|
||||||
|
return &dto.Request[ModelT]{
|
||||||
|
Body: body,
|
||||||
|
User: cinfo,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -1,94 +0,0 @@
|
|||||||
// 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 (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ProfileController interface {
|
|
||||||
GetProfile(c *gin.Context)
|
|
||||||
GetOwnProfile(c *gin.Context)
|
|
||||||
UpdateProfile(c *gin.Context)
|
|
||||||
GetPrivacySettings(c *gin.Context)
|
|
||||||
UpdatePrivacySettings(c *gin.Context)
|
|
||||||
Router
|
|
||||||
}
|
|
||||||
|
|
||||||
type profileControllerImpl struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
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]
|
|
||||||
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]
|
|
||||||
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]
|
|
||||||
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]
|
|
||||||
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]
|
|
||||||
func (p *profileControllerImpl) UpdatePrivacySettings(c *gin.Context) {
|
|
||||||
c.Status(http.StatusNotImplemented)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *profileControllerImpl) RegisterRoutes(group *gin.RouterGroup) {
|
|
||||||
}
|
|
||||||
@@ -18,39 +18,43 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"easywish/internal/models"
|
||||||
|
"easywish/internal/utils/enums"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServiceController interface {
|
type ServiceController struct {}
|
||||||
HealthCheck(c *gin.Context)
|
|
||||||
Router
|
func NewServiceController() Controller {
|
||||||
|
|
||||||
|
ctrl := &ServiceController{}
|
||||||
|
|
||||||
|
return &controllerImpl{
|
||||||
|
Path: "/service",
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Methods: []ControllerMethod{
|
||||||
|
|
||||||
|
{
|
||||||
|
HttpMethod: GET,
|
||||||
|
Path: "/health",
|
||||||
|
Authorization: enums.GuestRole,
|
||||||
|
Middleware: []gin.HandlerFunc{},
|
||||||
|
Function: ctrl.healthHandler,
|
||||||
|
},
|
||||||
|
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type serviceControllerImpl struct{}
|
// @Summary Get health status
|
||||||
|
// @Description Used internally for checking service health
|
||||||
func NewServiceController() ServiceController {
|
// @Tags Service
|
||||||
return &serviceControllerImpl{}
|
// @Accept json
|
||||||
}
|
// @Produce json
|
||||||
|
// @Success 200 {object} models.HealthStatusResponse "Says whether it's healthy or not"
|
||||||
// HealthCheck implements ServiceController.
|
// @Router /service/health [get]
|
||||||
// @Summary Get health status
|
func (ctrl *ServiceController) healthHandler(c *gin.Context) {
|
||||||
// @Description Used internally for checking service health
|
c.JSON(http.StatusOK, models.HealthStatusResponse{Healthy: true,})
|
||||||
// @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"`
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,13 +18,55 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"easywish/internal/dto"
|
||||||
|
"easywish/internal/middleware"
|
||||||
|
"easywish/internal/services"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"go.uber.org/fx"
|
"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",
|
var Module = fx.Module("controllers",
|
||||||
fx.Provide(
|
fx.Provide(
|
||||||
NewServiceController,
|
fx.Annotate(NewAuthController, fx.ResultTags(`group:"controllers"`)),
|
||||||
NewAuthController,
|
fx.Annotate(NewServiceController, fx.ResultTags(`group:"controllers"`)),
|
||||||
NewProfileController,
|
),
|
||||||
),
|
fx.Invoke(setupControllers),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,12 +15,10 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package controllers
|
package errors
|
||||||
|
|
||||||
import (
|
import "errors"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
|
var (
|
||||||
|
ErrClientInfoNotProvided = errors.New("No client info provded")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Router interface {
|
|
||||||
RegisterRoutes(group *gin.RouterGroup)
|
|
||||||
}
|
|
||||||
@@ -30,6 +30,7 @@ import (
|
|||||||
|
|
||||||
const requestKey = "request"
|
const requestKey = "request"
|
||||||
|
|
||||||
|
// Deprecated: no longer used, embedded into controllers.GetRequest instead
|
||||||
func ClientInfoFromContext(c *gin.Context) (*dto.ClientInfo, bool) {
|
func ClientInfoFromContext(c *gin.Context) (*dto.ClientInfo, bool) {
|
||||||
|
|
||||||
var ok bool
|
var ok bool
|
||||||
@@ -58,10 +59,12 @@ func ClientInfoFromContext(c *gin.Context) (*dto.ClientInfo, bool) {
|
|||||||
}, true
|
}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers.GetRequest
|
||||||
func RequestFromContext[T any](c *gin.Context) dto.Request[T] {
|
func RequestFromContext[T any](c *gin.Context) dto.Request[T] {
|
||||||
return c.Value(requestKey).(dto.Request[T])
|
return c.Value(requestKey).(dto.Request[T])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers.GetRequest
|
||||||
func RequestMiddleware[T any](role enums.Role) gin.HandlerFunc {
|
func RequestMiddleware[T any](role enums.Role) gin.HandlerFunc {
|
||||||
return gin.HandlerFunc(func(c *gin.Context) {
|
return gin.HandlerFunc(func(c *gin.Context) {
|
||||||
|
|
||||||
|
|||||||
@@ -15,20 +15,8 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package controllers
|
package models
|
||||||
|
|
||||||
import (
|
type HealthStatusResponse struct {
|
||||||
"net/http"
|
Healthy bool `json:"healthy"`
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
|
||||||
|
|
||||||
// @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)
|
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,7 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers
|
||||||
func NewRouter(engine *gin.Engine, log *zap.Logger, auth services.AuthService, groups []RouteGroup) *gin.Engine {
|
func NewRouter(engine *gin.Engine, log *zap.Logger, auth services.AuthService, groups []RouteGroup) *gin.Engine {
|
||||||
apiGroup := engine.Group("/api")
|
apiGroup := engine.Group("/api")
|
||||||
apiGroup.Use(middleware.AuthMiddleware(log, auth))
|
apiGroup.Use(middleware.AuthMiddleware(log, auth))
|
||||||
@@ -37,29 +38,30 @@ func NewRouter(engine *gin.Engine, log *zap.Logger, auth services.AuthService, g
|
|||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers
|
||||||
type RouteGroup struct {
|
type RouteGroup struct {
|
||||||
BasePath string
|
BasePath string
|
||||||
Middleware []gin.HandlerFunc
|
Middleware []gin.HandlerFunc
|
||||||
Router controllers.Router
|
Router controllers.Router
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouteGroups(
|
// func NewRouteGroups(
|
||||||
authController controllers.AuthController,
|
// authController controllers.AuthController,
|
||||||
serviceController controllers.ServiceController,
|
// serviceController controllers.ServiceController,
|
||||||
profileController controllers.ProfileController,
|
// profileController controllers.ProfileController,
|
||||||
) []RouteGroup {
|
// ) []RouteGroup {
|
||||||
return []RouteGroup{
|
// return []RouteGroup{
|
||||||
{
|
// {
|
||||||
BasePath: "/auth",
|
// BasePath: "/auth",
|
||||||
Router: authController,
|
// Router: authController,
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
BasePath: "/service",
|
// BasePath: "/service",
|
||||||
Router: serviceController,
|
// Router: serviceController,
|
||||||
},
|
// },
|
||||||
{
|
// {
|
||||||
BasePath: "/profile",
|
// BasePath: "/profile",
|
||||||
Router: profileController,
|
// Router: profileController,
|
||||||
},
|
// },
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -21,9 +21,10 @@ import (
|
|||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: no longer used, see controllers
|
||||||
var Module = fx.Module("routes",
|
var Module = fx.Module("routes",
|
||||||
fx.Provide(
|
// fx.Provide(
|
||||||
NewRouteGroups,
|
// NewRouteGroups,
|
||||||
),
|
// ),
|
||||||
fx.Invoke(NewRouter),
|
// fx.Invoke(NewRouter),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: use controllers.GetRequest method for the new controllers
|
||||||
func GetRequest[T any](c *gin.Context) (*dto.Request[T], bool) {
|
func GetRequest[T any](c *gin.Context) (*dto.Request[T], bool) {
|
||||||
|
|
||||||
req, ok := c.Get("request")
|
req, ok := c.Get("request")
|
||||||
|
|||||||
Reference in New Issue
Block a user