Skip to content

Commit

Permalink
feat: add random captcha
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Mar 28, 2021
1 parent 8a43ded commit d00ba0c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions captcha/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package captcha

import (
"math/rand"
)

func NewRandomCaptcha() Captcha {
availables := []func() Captcha{
NewBannerCaptcha,
NewMathCaptcha,
}
return availables[rand.Intn(len(availables))]() // nolint:gosec
}
30 changes: 30 additions & 0 deletions captcha/random_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package captcha_test

import (
"fmt"
"math/rand"

"moul.io/captcha/captcha"
)

func ExampleNewRandomCaptcha() {
rand.Seed(42)

// first captcha
c := captcha.NewRandomCaptcha()
question, _ := c.Question()
fmt.Println(question)

// second captcha
c = captcha.NewRandomCaptcha()
question, _ = c.Question()
fmt.Println(question)

// Output:
// 3 + 4
// _ _
// _ __ | |_ | |_ _ _ ___
// | '_ \| _|| _|| || |/ -_)
// | .__/ \__| \__| \_,_|\___|
// |_|
}

0 comments on commit d00ba0c

Please sign in to comment.