sipd-auth/handler/http/captcha.go
2025-09-16 08:32:11 +07:00

101 lines
3.0 KiB
Go

package http
import (
"kemendagri/sipd/services/sipd_auth/controller"
models "kemendagri/sipd/services/sipd_auth/model"
"github.com/gofiber/fiber/v2"
)
type CaptchaHandler struct {
Controller *controller.CaptchaController
}
func NewCaptchaHandler(app *fiber.App, controller *controller.CaptchaController) {
handler := &CaptchaHandler{
Controller: controller,
}
// public route
rPub := app.Group("/captcha")
rPub.Get("/new", handler.New)
rPub.Get("/reload/:id", handler.Reload)
rPub.Post("/validate", handler.Validate)
}
// New func for generate new captcha.
//
// @Summary generate new captcha
// @Description generate new captcha.
// @Tags Captcha
// @Accept json
// @Produce json
// @Success 200 {object} interface{} "Base64 image string"
// @Failure 400 {object} utils.RequestError "Bad request"
// @Failure 403 {object} utils.LoginError "Login forbidden"
// @Failure 404 {object} utils.RequestError "Data not found"
// @Failure 422 {array} utils.RequestError "Data validation failed"
// @Failure 500 {object} utils.RequestError "Server error"
// @Router /captcha/new [get]
func (h *CaptchaHandler) New(c *fiber.Ctx) error {
id, base64, audio, err := h.Controller.New()
if err != nil {
return err
}
return c.JSON(fiber.Map{"id": id, "base64": base64, "audio": audio})
}
// Reload func for reload captcha.
//
// @Summary reload captcha
// @Description reload captcha.
// @Tags Captcha
// @Accept json
// @Produce json
// @Param id path string true "captcha ID"
// @Success 200 {object} interface{} "Base64 image string"
// @Failure 400 {object} utils.RequestError "Bad request"
// @Failure 403 {object} utils.LoginError "Login forbidden"
// @Failure 404 {object} utils.RequestError "Data not found"
// @Failure 422 {array} utils.RequestError "Data validation failed"
// @Failure 500 {object} utils.RequestError "Server error"
// @Router /captcha/reload/{id} [get]
func (h *CaptchaHandler) Reload(c *fiber.Ctx) error {
base64, audio, err := h.Controller.Reload(c.Params("id"))
if err != nil {
return err
}
return c.JSON(fiber.Map{"base64": base64, "audio": audio})
}
// Validate func for validate captcha.
//
// @Summary validate captcha
// @Description validate captcha.
// @Tags Captcha
// @Accept json
// @Produce json
// @Param payload body models.ValidateCaptcha true "payload"
// @Success 200 {object} boolean "validate success"
// @Failure 400 {object} utils.RequestError "Bad request"
// @Failure 403 {object} utils.LoginError "Login forbidden"
// @Failure 404 {object} utils.RequestError "Data not found"
// @Failure 422 {array} utils.RequestError "Data validation failed"
// @Failure 500 {object} utils.RequestError "Server error"
// @Router /captcha/validate [post]
func (h *CaptchaHandler) Validate(c *fiber.Ctx) error {
formModel := new(models.ValidateCaptcha)
if err := c.BodyParser(formModel); err != nil {
return err
}
err := h.Controller.Validate(*formModel)
if err != nil {
return err
}
return c.JSON(true)
}