2017-09-19 03:10:02 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/steambap/captcha"
|
2017-10-07 09:13:12 +01:00
|
|
|
"golang.org/x/image/font/gofont/goregular"
|
2017-09-19 03:10:02 +01:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-10-07 09:13:12 +01:00
|
|
|
err := captcha.LoadFont(goregular.TTF)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-09-19 07:10:49 +01:00
|
|
|
http.HandleFunc("/", indexHandle)
|
2017-10-07 09:13:12 +01:00
|
|
|
http.HandleFunc("/captcha", captchaHandle)
|
2017-09-19 03:10:02 +01:00
|
|
|
fmt.Println("Server start at port 8080")
|
2017-10-07 09:13:12 +01:00
|
|
|
err = http.ListenAndServe(":8080", nil)
|
2017-09-19 03:10:02 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 07:10:49 +01:00
|
|
|
func indexHandle(w http.ResponseWriter, _ *http.Request) {
|
2017-09-19 03:10:02 +01:00
|
|
|
doc, err := template.ParseFiles("index.html")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
doc.Execute(w, nil)
|
|
|
|
}
|
|
|
|
|
2017-09-19 07:10:49 +01:00
|
|
|
func captchaHandle(w http.ResponseWriter, _ *http.Request) {
|
2017-10-07 09:13:12 +01:00
|
|
|
img, err := captcha.New(150, 50, func(options *captcha.Options) {
|
2017-10-08 03:25:22 +01:00
|
|
|
options.FontScale = 0.8
|
2017-10-07 09:13:12 +01:00
|
|
|
})
|
2017-09-19 03:10:02 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(w, nil)
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
img.WriteTo(w)
|
2017-10-08 03:25:22 +01:00
|
|
|
}
|