example/main.go (view raw)
1// example of HTTP server that uses the captcha package.
2package main
3
4import (
5 "fmt"
6 "github.com/dchest/captcha"
7 "http"
8 "io"
9 "log"
10 "template"
11)
12
13var formTemplate = template.MustParse(formTemplateSrc, nil)
14
15func showFormHandler(w http.ResponseWriter, r *http.Request) {
16 if r.URL.Path != "/" {
17 http.NotFound(w, r)
18 return
19 }
20 d := struct {
21 CaptchaId string
22 JavaScript string
23 }{
24 captcha.New(),
25 formJavaScript,
26 }
27 if err := formTemplate.Execute(w, &d); err != nil {
28 http.Error(w, err.String(), http.StatusInternalServerError)
29 }
30}
31
32func processFormHandler(w http.ResponseWriter, r *http.Request) {
33 if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
34 io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
35 } else {
36 io.WriteString(w, "Great job, human! You solved the captcha.\n")
37 }
38 io.WriteString(w, "<br><a href='/'>Try another one</a>")
39}
40
41func main() {
42 http.HandleFunc("/", showFormHandler)
43 http.HandleFunc("/process", processFormHandler)
44 http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
45 fmt.Println("Server is at localhost:8666")
46 if err := http.ListenAndServe(":8666", nil); err != nil {
47 log.Fatal(err)
48 }
49}
50
51const formJavaScript = `
52function setSrcQuery(e, q) {
53 var src = e.src;
54 var p = src.indexOf('?');
55 if (p >= 0) {
56 src = src.substr(0, p);
57 }
58 e.src = src + "?" + q
59}
60
61function playAudio() {
62 var le = document.getElementById("lang");
63 var lang = le.options[le.selectedIndex].value;
64 var e = document.getElementById('audio')
65 setSrcQuery(e, "lang=" + lang)
66 e.style.display = 'block';
67 e.autoplay = 'true';
68 return false;
69}
70
71function changeLang() {
72 var e = document.getElementById('audio')
73 if (e.style.display == 'block') {
74 playAudio();
75 }
76}
77
78function reload() {
79 setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
80 setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
81 return false;
82}
83`
84
85const formTemplateSrc = `<!doctype html>
86<head><title>Captcha Example</title></head>
87<body>
88<script>
89{JavaScript}
90</script>
91<select id="lang" onchange="changeLang()">
92 <option value="en">English</option>
93 <option value="ru">Russian</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`