all repos — captcha @ 73e6160e7aefcff4f2aa991310f9d3287de0bbeb

Go package captcha implements generation and verification of image and audio CAPTCHAs.

capexample/main.go (view raw)

  1// Copyright 2011 Dmitry Chestnykh. All rights reserved.
  2// Use of this source code is governed by a MIT-style
  3// license that can be found in the LICENSE file.
  4
  5// example of HTTP server that uses the captcha package.
  6package main
  7
  8import (
  9	"fmt"
 10	"github.com/dchest/captcha"
 11	"io"
 12	"log"
 13	"net/http"
 14	"text/template"
 15)
 16
 17var formTemplate = template.Must(template.New("example").Parse(formTemplateSrc))
 18
 19func showFormHandler(w http.ResponseWriter, r *http.Request) {
 20	if r.URL.Path != "/" {
 21		http.NotFound(w, r)
 22		return
 23	}
 24	d := struct {
 25		CaptchaId string
 26	}{
 27		captcha.New(),
 28	}
 29	if err := formTemplate.Execute(w, &d); err != nil {
 30		http.Error(w, err.Error(), http.StatusInternalServerError)
 31	}
 32}
 33
 34func processFormHandler(w http.ResponseWriter, r *http.Request) {
 35	w.Header().Set("Content-Type", "text/html; charset=utf-8")
 36	if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
 37		io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
 38	} else {
 39		io.WriteString(w, "Great job, human! You solved the captcha.\n")
 40	}
 41	io.WriteString(w, "<br><a href='/'>Try another one</a>")
 42}
 43
 44func main() {
 45	http.HandleFunc("/", showFormHandler)
 46	http.HandleFunc("/process", processFormHandler)
 47	http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
 48	fmt.Println("Server is at localhost:8666")
 49	if err := http.ListenAndServe(":8666", nil); err != nil {
 50		log.Fatal(err)
 51	}
 52}
 53
 54const formTemplateSrc = `<!doctype html>
 55<head><title>Captcha Example</title></head>
 56<body>
 57<script>
 58function setSrcQuery(e, q) {
 59	var src  = e.src;
 60	var p = src.indexOf('?');
 61	if (p >= 0) {
 62		src = src.substr(0, p);
 63	}
 64	e.src = src + "?" + q
 65}
 66
 67function playAudio() {
 68	var le = document.getElementById("lang");
 69	var lang = le.options[le.selectedIndex].value;
 70	var e = document.getElementById('audio')
 71	setSrcQuery(e, "lang=" + lang)
 72	e.style.display = 'block';
 73	e.autoplay = 'true';
 74	return false;
 75}
 76
 77function changeLang() {
 78	var e = document.getElementById('audio')
 79	if (e.style.display == 'block') {
 80		playAudio();
 81	}
 82}
 83
 84function reload() {
 85	setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
 86	setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
 87	return false;
 88}
 89</script>
 90<select id="lang" onchange="changeLang()">
 91	<option value="en">English</option>
 92	<option value="ru">Russian</option>
 93	<option value="zh">Chinese</option>
 94</select>
 95<form action="/process" method=post>
 96<p>Type the numbers you see in the picture below:</p>
 97<p><img id=image src="/captcha/{{.CaptchaId}}.png" alt="Captcha image"></p>
 98<a href="#" onclick="reload()">Reload</a> | <a href="#" onclick="playAudio()">Play Audio</a>
 99<audio id=audio controls style="display:none" src="/captcha/{{.CaptchaId}}.wav" preload=none>
100  You browser doesn't support audio.
101  <a href="/captcha/download/{{.CaptchaId}}.wav">Download file</a> to play it in the external player.
102</audio>
103<input type=hidden name=captchaId value="{{.CaptchaId}}"><br>
104<input name=captchaSolution>
105<input type=submit value=Submit>
106</form>
107`