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/basic/main.go

49 lines
934 B
Go
Raw Normal View History

2017-09-19 03:10:02 +01:00
package main
import (
"fmt"
"github.com/steambap/captcha"
"html/template"
"net/http"
)
func main() {
http.HandleFunc("/", indexHandle)
http.HandleFunc("/captcha-default", captchaHandle)
http.HandleFunc("/captcha-math", mathHandle)
2017-09-19 03:10:02 +01:00
fmt.Println("Server start at port 8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err)
}
}
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)
}
func captchaHandle(w http.ResponseWriter, _ *http.Request) {
2017-09-19 03:10:02 +01:00
img, err := captcha.New(150, 50)
if err != nil {
fmt.Fprint(w, nil)
fmt.Println(err.Error())
return
}
img.WriteTo(w)
}
func mathHandle(w http.ResponseWriter, _ *http.Request) {
img, err := captcha.NewMathExpr(150, 50)
if err != nil {
fmt.Fprint(w, nil)
fmt.Println(err.Error())
return
}
img.WriteTo(w)
}