Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions backend/biz/public/handler/http/v1/captcha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package v1

import (
"log/slog"
"net/http"

"github.com/GoYoko/web"
gocap "github.com/ackcoder/go-cap"
"github.com/samber/do"

"github.com/chaitin/MonkeyCode/backend/domain"
"github.com/chaitin/MonkeyCode/backend/errcode"
"github.com/chaitin/MonkeyCode/backend/pkg/captcha"
)

type CaptchaHandler struct {
cap *captcha.Captcha
logger *slog.Logger
}

func NewCaptchaHandler(i *do.Injector) (*CaptchaHandler, error) {
w := do.MustInvoke[*web.Web](i)

c := &CaptchaHandler{
cap: do.MustInvoke[*captcha.Captcha](i),
logger: do.MustInvoke[*slog.Logger](i).With("module", "CaptchaHandler"),
}

v1 := w.Group("/api/v1/public/captcha")
v1.POST("/challenge", web.BaseHandler(c.CreateCaptcha))
v1.POST("/redeem", web.BindHandler(c.RedeemCaptcha))

return c, nil
}

// CreateCaptcha
//
// @Summary CreateCaptcha
// @Description CreateCaptcha
// @Tags 【验证码】
// @Accept json
// @Produce json
// @Success 200 {object} gocap.ChallengeData
// @Router /api/v1/public/captcha/challenge [post]
func (h *CaptchaHandler) CreateCaptcha(c *web.Context) error {
data, err := h.cap.CreateChallenge(c.Request().Context())
if err != nil {
h.logger.ErrorContext(c.Request().Context(), "create captcha failed", "error", err)
return errcode.ErrCreateCaptchaFailed.Wrap(err)
}
return c.JSON(http.StatusCreated, data)
}

// RedeemCaptcha
//
// @Summary RedeemCaptcha
// @Description RedeemCaptcha
// @Tags 【验证码】
// @Accept json
// @Produce json
// @Param body body domain.RedeemCaptchaReq true "request"
// @Success 200 {object} gocap.VerificationResult
// @Router /api/v1/public/captcha/redeem [post]
func (h *CaptchaHandler) RedeemCaptcha(c *web.Context, req domain.RedeemCaptchaReq) error {
h.logger.InfoContext(c.Request().Context(), "redeem captcha", "req", req)

data, err := h.cap.RedeemChallenge(c.Request().Context(), req.Token, req.Solutions)
if err != nil {
return c.JSON(http.StatusInternalServerError, gocap.VerificationResult{
Success: false,
Message: err.Error(),
})
}
return c.JSON(http.StatusCreated, gocap.VerificationResult{
Success: true,
TokenData: data,
})
}
19 changes: 4 additions & 15 deletions backend/biz/public/register.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package public

import (
"github.com/GoYoko/web"
"github.com/samber/do"

"github.com/chaitin/MonkeyCode/backend/pkg/captcha"
v1 "github.com/chaitin/MonkeyCode/backend/biz/public/handler/http/v1"
)

// RegisterPublic 注册 public 模块
func RegisterPublic(i *do.Injector) error {
w := do.MustInvoke[*web.Web](i)
captchaSvc := do.MustInvoke[*captcha.Captcha](i)

// 验证码路由
v1 := w.Group("/api/v1/public")
v1.GET("/captcha", web.BaseHandler(func(c *web.Context) error {
return c.String(200, "captcha endpoint")
}))
_ = captchaSvc

return nil
func RegisterPublic(i *do.Injector) {
do.Provide(i, v1.NewCaptchaHandler)
do.MustInvoke[*v1.CaptchaHandler](i)
}
11 changes: 2 additions & 9 deletions backend/biz/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,8 @@ import (

// RegisterAll 注册所有 biz 模块
func RegisterAll(i *do.Injector) error {
// 注册 public 模块
if err := public.RegisterPublic(i); err != nil {
return err
}

// 注册 user 模块
if err := user.RegisterUser(i); err != nil {
return err
}
public.RegisterPublic(i)
user.RegisterUser(i)

// 注册 team 模块
if err := team.RegisterTeam(i); err != nil {
Expand Down
Loading
Loading