Debian Codegraph @

example/example.go

raw
1 // example.go
2 //
3 // A simple HTTP server which presents a reCaptcha input form and evaulates the result,
4 // using the github.com/dpapathanasiou/go-recaptcha package.
5 //
6 // See the main() function for usage.
7 package main
8
9 import (
10 "fmt"
11 "github.com/dpapathanasiou/go-recaptcha"
12 "log"
13 "net/http"
14 "os"
15 "path/filepath"
16 )
17
18 var recaptchaPublicKey string
19
20 const (
21 pageTop = `<!DOCTYPE HTML><html><head>
22 <style>.error{color:#ff0000;} .ack{color:#0000ff;}</style><title>Recaptcha Test</title></head>
23 <body><div style="width:100%"><div style="width: 50%;margin: 0 auto;">
24 <h3>Recaptcha Test</h3>
25 <p>This is a test form for the go-recaptcha package</p>`
26 form = `<form action="/" method="POST">
27 <script src="https://www.google.com/recaptcha/api.js"></script>
28 <div class="g-recaptcha" data-sitekey="%s"></div>
29 <input type="submit" name="button" value="Ok">
30 </form>`
31 pageBottom = `</div></div></body></html>`
32 anError = `<p class="error">%s</p>`
33 anAck = `<p class="ack">%s</p>`
34 )
35
36 // processRequest accepts the http.Request object, finds the reCaptcha form variables which
37 // were input and sent by HTTP POST to the server, then calls the recaptcha package's Confirm()
38 // method, which returns a boolean indicating whether or not the client answered the form correctly.
39 func processRequest(request *http.Request) (result bool) {
40 recaptchaResponse, responseFound := request.Form["g-recaptcha-response"]
41 if responseFound {
42 result, err := recaptcha.Confirm("127.0.0.1", recaptchaResponse[0])
43 if err != nil {
44 log.Println("recaptcha server error", err)
45 }
46 return result
47 }
48 return false
49 }
50
51 // homePage is a simple HTTP handler which produces a basic HTML page
52 // (as defined by the pageTop and pageBottom constants), including
53 // an input form with a reCaptcha challenge.
54 // If the http.Request object indicates the form input has been posted,
55 // it calls processRequest() and displays a message indicating whether or not
56 // the reCaptcha form was input correctly.
57 // Either way, it writes HTML output through the http.ResponseWriter.
58 func homePage(writer http.ResponseWriter, request *http.Request) {
59 err := request.ParseForm() // Must be called before writing response
60 fmt.Fprint(writer, pageTop)
61 if err != nil {
62 fmt.Fprintf(writer, fmt.Sprintf(anError, err))
63 } else {
64 _, buttonClicked := request.Form["button"]
65 if buttonClicked {
66 if processRequest(request) {
67 fmt.Fprint(writer, fmt.Sprintf(anAck, "Recaptcha was correct!"))
68 } else {
69 fmt.Fprintf(writer, fmt.Sprintf(anError, "Recaptcha was incorrect; try again."))
70 }
71 }
72 }
73 fmt.Fprint(writer, fmt.Sprintf(form, recaptchaPublicKey))
74 fmt.Fprint(writer, pageBottom)
75 }
76
77 // main expects two command-line arguments: the reCaptcha public key for producing the HTML form,
78 // and the reCaptcha private key, to pass to recaptcha.Init() so the recaptcha package can check the input.
79 // It launches a simple web server on port 9001 which produces the reCaptcha input form and checks the client
80 // input if the form is posted.
81 func main() {
82 if len(os.Args) != 3 {
83 fmt.Printf("usage: %s <reCaptcha public key> <reCaptcha private key>\n", filepath.Base(os.Args[0]))
84 os.Exit(1)
85 } else {
86 recaptchaPublicKey = os.Args[1]
87 recaptcha.Init(os.Args[2])
88
89 http.HandleFunc("/", homePage)
90 if err := http.ListenAndServe(":9001", nil); err != nil {
91 log.Fatal("failed to start server", err)
92 }
93 }
94 }