example/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 "http"
12 "io"
13 "log"
14 "template"
15)
16
17var formTemplate = template.MustParse(formTemplateSrc, nil)
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 JavaScript string
27 }{
28 captcha.New(),
29 formJavaScript,
30 }
31 if err := formTemplate.Execute(w, &d); err != nil {
32 http.Error(w, err.String(), http.StatusInternalServerError)
33 }
34}
35
36func processFormHandler(w http.ResponseWriter, r *http.Request) {
37 if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
38 io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
39 } else {
40 io.WriteString(w, "Great job, human! You solved the captcha.\n")
41 }
42 io.WriteString(w, "<br><a href='/'>Try another one</a>")
43}
44
45func main() {
46 http.HandleFunc("/", showFormHandler)
47 http.HandleFunc("/process", processFormHandler)
48 http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
49 fmt.Println("Server is at localhost:8666")
50 if err := http.ListenAndServe(":8666", nil); err != nil {
51 log.Fatal(err)
52 }
53}
54
55const formJavaScript = `
56function setSrcQuery(e, q) {
57 var src = e.src;
58 var p = src.indexOf('?');
59 if (p >= 0) {
60 src = src.substr(0, p);
61 }
62 e.src = src + "?" + q
63}
64
65function playAudio() {
66 var le = document.getElementById("lang");
67 var lang = le.options[le.selectedIndex].value;
68 var e = document.getElementById('audio')
69 setSrcQuery(e, "lang=" + lang)
70 e.style.display = 'block';
71 e.autoplay = 'true';
72 return false;
73}
74
75function changeLang() {
76 var e = document.getElementById('audio')
77 if (e.style.display == 'block') {
78 playAudio();
79 }
80}
81
82function reload() {
83 setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
84 setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
85 return false;
86}
87`
88
89const formTemplateSrc = `<!doctype html>
90<head><title>Captcha Example</title></head>
91<body>
92<script>
93{JavaScript}
94</script>
95<select id="lang" onchange="changeLang()">
96 <option value="en">English</option>
97 <option value="ru">Russian</option>
98</select>
99<form action="/process" method=post>
100<p>Type the numbers you see in the picture below:</p>
101<p><img id=image src="/captcha/{CaptchaId}.png" alt="Captcha image"></p>
102<a href="#" onclick="reload()">Reload</a> | <a href="#" onclick="playAudio()">Play Audio</a>
103<audio id=audio controls style="display:none" src="/captcha/{CaptchaId}.wav" preload=none>
104 You browser doesn't support audio.
105 <a href="/captcha/download/{CaptchaId}.wav">Download file</a> to play it in the external player.
106</audio>
107<input type=hidden name=captchaId value="{CaptchaId}"><br>
108<input name=captchaSolution>
109<input type=submit value=Submit>
110</form>
111`