feat: add captcha endpoint

This commit is contained in:
Yanislav Igonin 2021-10-03 11:48:59 +03:00
parent d4a3422281
commit e0d44aa0b6
5 changed files with 31 additions and 1 deletions

View File

@ -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())
}

View File

@ -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)
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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")