mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
Add example for Google reCAPTCAA middleware. Prev Commit: Update to 8.2.2. Read HISTORY.md
Former-commit-id: 04f8d4b96336506f49c9b4285112d638f1e8dd97
This commit is contained in:
45
_examples/miscellaneous/recaptcha/main.go
Normal file
45
_examples/miscellaneous/recaptcha/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
|
||||
"github.com/kataras/iris/middleware/recaptcha"
|
||||
)
|
||||
|
||||
// publicDataSiteKey and secretKey and should be obtained by https://www.google.com/recaptcha.
|
||||
const (
|
||||
publicDataSiteKey = ""
|
||||
secretKey = ""
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
r := recaptcha.New(secretKey)
|
||||
|
||||
app.Get("/comment", showRecaptchaForm)
|
||||
|
||||
// pass the middleware before the main handler or use the `recaptcha.SiteVerify`.
|
||||
app.Post("/comment", r, postComment)
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
var htmlForm = `<form action="/comment" method="POST">
|
||||
<script src="https://www.google.com/recaptcha/api.js"></script>
|
||||
<div class="g-recaptcha" data-sitekey="%s"></div>
|
||||
<input type="submit" name="button" value="Verify">
|
||||
</form>`
|
||||
|
||||
func showRecaptchaForm(ctx context.Context) {
|
||||
contents := fmt.Sprintf(htmlForm, publicDataSiteKey)
|
||||
ctx.HTML(contents)
|
||||
}
|
||||
|
||||
func postComment(ctx context.Context) {
|
||||
// [...]
|
||||
ctx.JSON(context.Map{"success": true})
|
||||
}
|
||||
Reference in New Issue
Block a user