Page:
Captcha
Pages
Actions
Basicauth
Binding
Captcha
Compress
Context
Debug
Dispatch
ErrHandler
Events
Flash
Forms
Group
Handler
Home
Injection
Logger
Params
QuickStart
Recovery
Renders
Return
Router
Session
Static
Tango
Tpongo2
Xsrf
ZH_Actions
ZH_Basicauth
ZH_Binding
ZH_Captcha
ZH_Compress
ZH_Context
ZH_Debug
ZH_Dispatch
ZH_ErrHandler
ZH_Events
ZH_Flash
ZH_Forms
ZH_Group
ZH_HOME
ZH_Handler
ZH_Injection
ZH_Logger
ZH_Params
ZH_Recovery
ZH_Renders
ZH_Return
ZH_Router
ZH_Session
ZH_Static
ZH_Tango
ZH_Tpongo2
ZH_Xsrf
2
Captcha
Lunny Xiao edited this page 2019-11-27 09:08:08 +08:00
Table of Contents
Middleware captcha a middleware that provides captcha service for Tango.
Installation
go get gitea.com/tango/captcha
Usage
// main.go
import (
"gitea.com/lunny/tango"
"github.com/macaron-contrib/cache"
"gitea.com/tango/captcha"
)
type CaptchaAction struct {
captcha.Captcha
renders.Renderer
}
func (c *CaptchaAction) Get() {
c.Render("captcha.html", renders.T{
"captcha": c.CreateHtml(),
})
}
func (c *CaptchaAction) Post() string {
if c.Verify() {
return "true"
}
return "false"
}
func main() {
t := tango.Classic()
c, _ := cache.NewCache("memory", `{"interval":120}`)
t.Use(captcha.New(captcha.Options{}, c))
t.Any("/", new(CaptchaAction))
t.Run()
}
<!-- templates/captcha.tmpl -->
{{.captcha}}
Options
captcha.Captchaer
comes with a variety of configuration options:
// ...
t.Use(captcha.New(captcha.Options{
URLPrefix: "/captcha/", // URL prefix of getting captcha pictures.
FieldIdName: "captcha_id", // Hidden input element ID.
FieldCaptchaName: "captcha", // User input value element name in request form.
ChallengeNums: 6, // Challenge number.
Width: 240, // Captcha image width.
Height: 80, // Captcha image height.
Expiration: 600, // Captcha expiration time in seconds.
CachePrefix: "captcha_", // Cache key prefix captcha characters.
}, cache))
// ...