This repository has been archived on 2024-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
captcha/example/load-font/main.go

47 lines
868 B
Go

package main
import (
"fmt"
"html/template"
"net/http"
"github.com/steambap/captcha"
"golang.org/x/image/font/gofont/goregular"
)
func main() {
err := captcha.LoadFont(goregular.TTF)
if err != nil {
panic(err)
}
http.HandleFunc("/", indexHandle)
http.HandleFunc("/captcha", captchaHandle)
fmt.Println("Server start at port 8080")
err = http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
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)
}