Skip to content
flamegoPublic

About

Package altcha is a middleware that provides ALTCHA service for Flamego

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

altcha

GitHub Workflow Status GoDoc

Package altcha is a middleware that provides self-hosted ALTCHA proof-of-work captchas for Flamego.

Installation

go get github.com/flamego/altcha

Getting started

<!-- templates/home.tmpl -->
<form method="POST">
  {{.AltchaHTML}}
  <button>Submit</button>
</form>
package main

import (
	"net/http"
	"os"

	"github.com/flamego/altcha"
	"github.com/flamego/flamego"
	"github.com/flamego/template"
)

func main() {
	f := flamego.Classic()
	f.Use(template.Templater())
	f.Use(altcha.Challenger(
		altcha.Options{
			Secret: []byte(os.Getenv("ALTCHA_SECRET")),
		},
	))
	f.Get("/", func(t template.Template, data template.Data, c altcha.Challenge) {
		data["AltchaHTML"] = c.HTML()
		t.HTML(http.StatusOK, "home")
	})
	f.Post("/", func(w http.ResponseWriter, r *http.Request, c altcha.Challenge) {
		if err := c.Verify(r.PostFormValue(altcha.FieldName)); err != nil {
			w.WriteHeader(http.StatusBadRequest)
			_, _ = w.Write([]byte(err.Error()))
			return
		}
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte("Verified!"))
	})

	f.Run()
}

Getting help

License

This project is under the MIT License. See the LICENSE file for the full license text.

About

Package altcha is a middleware that provides ALTCHA service for Flamego

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages