From d00ba0c4f1fe625eaf05d8a8cff8fd0f145f8414 Mon Sep 17 00:00:00 2001 From: Manfred Touron <94029+moul@users.noreply.github.com> Date: Sun, 28 Mar 2021 20:57:02 +0000 Subject: [PATCH] feat: add random captcha Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com> --- captcha/random.go | 13 +++++++++++++ captcha/random_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 captcha/random.go create mode 100644 captcha/random_test.go diff --git a/captcha/random.go b/captcha/random.go new file mode 100644 index 0000000..4a98798 --- /dev/null +++ b/captcha/random.go @@ -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 +} diff --git a/captcha/random_test.go b/captcha/random_test.go new file mode 100644 index 0000000..6803414 --- /dev/null +++ b/captcha/random_test.go @@ -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 + // _ _ + // _ __ | |_ | |_ _ _ ___ + // | '_ \| _|| _|| || |/ -_) + // | .__/ \__| \__| \_,_|\___| + // |_| +}