Merge pull request #1055 from trheyi/main
Add Captcha handlers to OpenAPI routing
This commit is contained in:
commit
d0b9d4f313
2 changed files with 61 additions and 0 deletions
57
openapi/captcha/captcha.go
Normal file
57
openapi/captcha/captcha.go
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
package captcha
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/yaoapp/yao/helper"
|
||||||
|
"github.com/yaoapp/yao/openapi/oauth/types"
|
||||||
|
"github.com/yaoapp/yao/openapi/response"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Attach attaches the hello world handlers to the router
|
||||||
|
func Attach(group *gin.RouterGroup, oauth types.OAuth) {
|
||||||
|
|
||||||
|
// Health check
|
||||||
|
group.GET("/image", image)
|
||||||
|
|
||||||
|
// OAuth Protected Resource
|
||||||
|
group.GET("/audio", audio)
|
||||||
|
}
|
||||||
|
|
||||||
|
// image captcha
|
||||||
|
func image(c *gin.Context) {
|
||||||
|
var option helper.CaptchaOption = helper.NewCaptchaOption()
|
||||||
|
|
||||||
|
err := c.ShouldBindQuery(&option)
|
||||||
|
if err != nil {
|
||||||
|
response.RespondWithError(c, http.StatusBadRequest, &response.ErrorResponse{
|
||||||
|
Code: response.ErrInvalidRequest.Code,
|
||||||
|
ErrorDescription: err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the type to image
|
||||||
|
option.Type = "image"
|
||||||
|
id, content := helper.CaptchaMake(option)
|
||||||
|
response.RespondWithSuccess(c, http.StatusOK, gin.H{"id": id, "data": content})
|
||||||
|
}
|
||||||
|
|
||||||
|
// audio captcha
|
||||||
|
func audio(c *gin.Context) {
|
||||||
|
var option helper.CaptchaOption = helper.NewCaptchaOption()
|
||||||
|
|
||||||
|
err := c.ShouldBindQuery(&option)
|
||||||
|
if err != nil {
|
||||||
|
response.RespondWithError(c, http.StatusBadRequest, &response.ErrorResponse{
|
||||||
|
Code: response.ErrInvalidRequest.Code,
|
||||||
|
ErrorDescription: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the type to audio
|
||||||
|
option.Type = "audio"
|
||||||
|
id, content := helper.CaptchaMake(option)
|
||||||
|
response.RespondWithSuccess(c, http.StatusOK, gin.H{"id": id, "data": content})
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/yaoapp/gou/application"
|
"github.com/yaoapp/gou/application"
|
||||||
"github.com/yaoapp/yao/config"
|
"github.com/yaoapp/yao/config"
|
||||||
|
"github.com/yaoapp/yao/openapi/captcha"
|
||||||
"github.com/yaoapp/yao/openapi/chat"
|
"github.com/yaoapp/yao/openapi/chat"
|
||||||
"github.com/yaoapp/yao/openapi/dsl"
|
"github.com/yaoapp/yao/openapi/dsl"
|
||||||
"github.com/yaoapp/yao/openapi/file"
|
"github.com/yaoapp/yao/openapi/file"
|
||||||
|
|
@ -100,5 +101,8 @@ func (openapi *OpenAPI) Attach(router *gin.Engine) {
|
||||||
// Signin handlers
|
// Signin handlers
|
||||||
signin.Attach(group, openapi.OAuth)
|
signin.Attach(group, openapi.OAuth)
|
||||||
|
|
||||||
|
// Captcha handlers
|
||||||
|
captcha.Attach(group.Group("/captcha"), openapi.OAuth)
|
||||||
|
|
||||||
// Custom handlers (Defined by developer)
|
// Custom handlers (Defined by developer)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue