-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (38 loc) · 871 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"html/template"
"net/http"
"github.com/pchchv/captcha"
"golang.org/x/image/font/gofont/goregular"
)
func indexHandle(w http.ResponseWriter, _ *http.Request) {
doc, err := template.ParseFiles("index.html")
if err != nil {
fmt.Fprint(w, err.Error())
return
}
doc.Execute(w, nil)
}
func captchaHandle(w http.ResponseWriter, _ *http.Request) {
img, err := captcha.New(150, 50, func(options *captcha.Options) {
options.FontScale = 0.8
})
if err != nil {
fmt.Fprint(w, nil)
fmt.Println(err.Error())
return
}
img.WriteImage(w)
}
func main() {
if err := captcha.LoadFont(goregular.TTF); err != nil {
panic(err)
}
http.HandleFunc("/", indexHandle)
http.HandleFunc("/captcha", captchaHandle)
fmt.Println("Server start at port 8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}