Files
artalkjs/server/handler/captcha_get.go
qwqcode cf7725edba refactor(go): bump the module version and support go install (#979)
BREAKING CHANGE: The original Go module name was `github.com/ArtalkJS/Artalk`, which has now been modified to `github.com/artalkjs/artalk/v2` (all lowercase). This change may affect the CI pipeline or other areas if you are compiling the Artalk server and have specified the fixed Go module name. Regarding the original `-ldflags -X github.com/ArtalkJS/Artalk/internal/config.Version` and `CommitHash` definitions, please remove them, as they are now obsolete. The `Version` has been hardcoded into the Go code, and the `CommitHash` is now read from the Go build info via runtime/debug’s vcs.revision value. This change only affects the server developers and does not impact the frontend UI client.
2024-09-18 21:43:43 +08:00

51 lines
1.4 KiB
Go

package handler
import (
"github.com/artalkjs/artalk/v2/internal/captcha"
"github.com/artalkjs/artalk/v2/internal/core"
"github.com/artalkjs/artalk/v2/server/common"
"github.com/gofiber/fiber/v2"
)
type ResponseCaptchaGet struct {
ImgData string `json:"img_data"`
}
// @Id GetCaptcha
// @Summary Get Captcha
// @Description Get a base64 encoded captcha image or a HTML page to verify for user
// @Tags Captcha
// @Produce json
// @Success 200 {object} ResponseCaptchaGet
// @Failure 500 {object} Map{msg=string}
// @Router /captcha [get]
func CaptchaGet(app *core.App, router fiber.Router) {
router.Get("/captcha", func(c *fiber.Ctx) error {
// create new captcha checker instance
cap := common.NewCaptchaChecker(app, c)
// get new captcha
got, err := cap.Get()
if err != nil {
return common.RespError(c, 500, "captcha generate err: "+err.Error())
}
// response captcha
switch cap.Type() {
case captcha.Image:
return common.RespData(c, ResponseCaptchaGet{
ImgData: string(got),
})
case captcha.IFrame:
c.Set(fiber.HeaderCacheControl, "no-cache, no-store, must-revalidate") // disable cache
c.Set(fiber.HeaderPragma, "no-cache")
c.Set(fiber.HeaderExpires, "0")
c.Set(fiber.HeaderContentType, "text/html") // response html body
return c.Send(got)
}
return common.RespError(c, 500, "invalid captcha type")
})
}