mirror of
https://github.com/kataras/iris.git
synced 2025-12-22 04:17:03 +00:00
Add a better example for the recaptcha middleware as requested at: https://github.com/kataras/iris/issues/799
Former-commit-id: 85c3286a9d6be5cf47631e7608f70f3790934e64
This commit is contained in:
44
_examples/miscellaneous/recaptcha/custom_form/main.go
Normal file
44
_examples/miscellaneous/recaptcha/custom_form/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
|
||||
"github.com/kataras/iris/middleware/recaptcha"
|
||||
)
|
||||
|
||||
// keys should be obtained by https://www.google.com/recaptcha
|
||||
const (
|
||||
recaptchaPublic = "6Lf3WywUAAAAAKNfAm5DP2J5ahqedtZdHTYaKkJ6"
|
||||
recaptchaSecret = "6Lf3WywUAAAAAJpArb8nW_LCL_PuPuokmEABFfgw"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
r := recaptcha.New(recaptchaSecret)
|
||||
|
||||
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 iris.Context) {
|
||||
contents := fmt.Sprintf(htmlForm, recaptchaPublic)
|
||||
ctx.HTML(contents)
|
||||
}
|
||||
|
||||
func postComment(ctx iris.Context) {
|
||||
// [...]
|
||||
ctx.JSON(iris.Map{"success": true})
|
||||
}
|
||||
Reference in New Issue
Block a user