// example of HTTP server that uses the captcha package. package main import ( "fmt" "github.com/dchest/captcha" "http" "template" ) var formTemplate = template.MustParse(formTemplateSrc, nil) func showFormHandler(w http.ResponseWriter, r *http.Request) { d := struct{ CaptchaId string }{captcha.New(captcha.StdLength)} fmt.Fprintf(w, formJs) if err := formTemplate.Execute(w, &d); err != nil { http.Error(w, err.String(), http.StatusInternalServerError) } } func processFormHandler(w http.ResponseWriter, r *http.Request) { if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) { fmt.Fprintf(w, "Wrong captcha solution! No robots allowed!") return } fmt.Fprintf(w, "Great job, human! You solved the captcha.") } func main() { http.HandleFunc("/", showFormHandler) http.HandleFunc("/process", processFormHandler) http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight)) http.ListenAndServe(":8080", nil) } const formJs = ` ` const formTemplateSrc = `

Type the numbers you see in the picture below:

Captcha image

Reload | Play Audio
`