// example of HTTP server that uses the captcha package. package main import ( "fmt" "github.com/dchest/captcha" "http" "io" "log" "template" ) var formTemplate = template.MustParse(formTemplateSrc, nil) func showFormHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } d := struct { CaptchaId string JavaScript string }{ captcha.New(), formJavaScript, } 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")) { io.WriteString(w, "Wrong captcha solution! No robots allowed!\n") } else { io.WriteString(w, "Great job, human! You solved the captcha.\n") } io.WriteString(w, "
Try another one") } func main() { http.HandleFunc("/", showFormHandler) http.HandleFunc("/process", processFormHandler) http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight)) fmt.Println("Server is at localhost:8666") if err := http.ListenAndServe(":8666", nil); err != nil { log.Fatal(err) } } const formJavaScript = ` function setSrcQuery(e, q) { var src = e.src; var p = src.indexOf('?'); if (p >= 0) { src = src.substr(0, p); } e.src = src + "?" + q } function playAudio() { var le = document.getElementById("lang"); var lang = le.options[le.selectedIndex].value; var e = document.getElementById('audio') setSrcQuery(e, "lang=" + lang) e.style.display = 'block'; e.autoplay = 'true'; return false; } function changeLang() { var e = document.getElementById('audio') if (e.style.display == 'block') { playAudio(); } } function reload() { setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime()); setSrcQuery(document.getElementById('audio'), (new Date()).getTime()); return false; } ` const formTemplateSrc = ` Captcha Example

Type the numbers you see in the picture below:

Captcha image

Reload | Play Audio
`