From e0d44aa0b6f98317f152924bdd615b0d0ce0c643 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Sun, 3 Oct 2021 11:48:59 +0300 Subject: [PATCH] feat: add captcha endpoint --- controllers/captcha_controller.go | 23 +++++++++++++++++++++++ controllers/threads_controller.go | 5 ++++- go.mod | 1 + go.sum | 2 ++ main.go | 1 + 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 controllers/captcha_controller.go diff --git a/controllers/captcha_controller.go b/controllers/captcha_controller.go new file mode 100644 index 0000000..fbc27ad --- /dev/null +++ b/controllers/captcha_controller.go @@ -0,0 +1,23 @@ +package controllers + +import ( + "bytes" + "log" + "net/http" + + "github.com/dchest/captcha" + "github.com/gin-gonic/gin" +) + +func GetCaptcha(c *gin.Context) { + ID := c.Param("captchaID") + var content bytes.Buffer + err := captcha.WriteImage(&content, ID, captcha.StdWidth, captcha.StdHeight) + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusInternalServerError, "500.html", nil) + return + } + + c.Data(200, "image/png", content.Bytes()) +} diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 0407f37..db43dcb 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -1,4 +1,4 @@ -package controlers +package controllers import ( "context" @@ -9,6 +9,7 @@ import ( "strconv" "strings" + "github.com/dchest/captcha" "github.com/gin-gonic/gin" csrf "github.com/utrack/gin-csrf" @@ -58,6 +59,8 @@ func GetThreads(c *gin.Context) { csrfToken := csrf.GetToken(c) c.SetCookie("csrf", csrfToken, 60, "/", "", true, true) + captchaID := captcha.New() + log.Println(captchaID) c.HTML(http.StatusOK, "index.html", data) } diff --git a/go.mod b/go.mod index 6f55c8a..b5f38ab 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module micrach go 1.15 require ( + github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f github.com/disintegration/imaging v1.6.2 github.com/gin-contrib/sessions v0.0.0-20190101140330-dc5246754963 github.com/gin-gonic/gin v1.7.4 diff --git a/go.sum b/go.sum index cba46bc..9ad9409 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f h1:q/DpyjJjZs94bziQ7YkBmIlpqbVP7yw179rnzoNVX1M= +github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f/go.mod h1:QGrK8vMWWHQYQ3QU9bw9Y9OPNfxccGzfb41qjvVeXtY= github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9 h1:74lLNRzvsdIlkTgfDSMuaPjBr4cf6k7pwQQANm/yLKU= github.com/dchest/uniuri v0.0.0-20160212164326-8902c56451e9/go.mod h1:GgB8SF9nRG+GqaDtLcwJZsQFhcogVCJ79j4EdT0c2V4= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= diff --git a/main.go b/main.go index 2727080..4ddd637 100644 --- a/main.go +++ b/main.go @@ -86,6 +86,7 @@ func main() { router.POST("/", Controllers.CreateThread) router.GET("/:threadID", Controllers.GetThread) router.POST("/:threadID", Controllers.UpdateThread) + router.GET("/captcha/:captchaID", Controllers.GetCaptcha) log.Println("port", Config.App.Port, "- online") log.Println("all systems nominal")