Skip to content

Commit

Permalink
etcd store example
Browse files Browse the repository at this point in the history
  • Loading branch information
JJJJJJJerk committed Mar 30, 2020
1 parent fc96cb7 commit b59c8f1
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ func main() {
}
```

#### 2.3.7 Example Use Etcd as store
[captcha with etcd database as store](captcha_with_etcd_exmaple.md)

## 3. 🎨🎨🎨 Customization
You can customize your captcha display image by implementing [interface driver](interface_driver.go)
Expand Down
111 changes: 111 additions & 0 deletions captcha_with_etcd_exmaple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Captcha with Etcd as store example


## captcha/captcha_etcd.go
```go
package captcha

import (
"context"
"github.com/mojocn/base64Captcha"
"go.etcd.io/etcd/clientv3"
"image/color"
"library/database/etcd"
"time"
)

//CaptchaEtcd base64 captcha with etcd
type CaptchaEtcd struct {
*base64Captcha.DriverString
store *etcd.Client
}

//NewClientEtcd constructor
func NewClientEtcd(height, width int, store *etcd.Client) *CaptchaEtcd {
d := base64Captcha.NewDriverString(height, width, 0, 0, 4, "%#=qwe23456789rtyupasdfghjkzxcvbnm", &color.RGBA{0, 0, 0, 0}, []string{"wqy-microhei.ttc"})
cli := &CaptchaEtcd{store: store}
cli.DriverString = d
return cli
}

const (
captchaPrefix = "captcha:"
requestTimeout = time.Second
)

//GenerateIdAndImage create image
func (c *CaptchaEtcd) GenerateIdAndImage() (id, b64s, ans string, err error) {
id, content, answer := c.GenerateIdQuestionAnswer()
item, err := c.DrawCaptcha(content)
if err != nil {
return "", "", "", err
}
//expire in 120s
grantResp, err := c.store.Grant(context.TODO(), 120)
if err != nil {
return "", "", "", err
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
_, err = c.store.Put(ctx, captchaPrefix+id, answer, clientv3.WithLease(grantResp.ID))
cancel()
if err != nil {
return "", "", "", err
}
b64s = item.EncodeB64string()
return id, b64s, answer, nil
}

//Verify check captcha answer
func (c *CaptchaEtcd) Verify(id, answer string) (match bool, err error) {
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
key := captchaPrefix + id
resp, err := c.store.Get(ctx, key)
cancel()
if err != nil {
return false, err
}

for _, ev := range resp.Kvs {
if string(ev.Value) == answer {
return true, err
}
}
return false, err
}

```

## captcha\captcha_etcd_test.go

```go
package captcha

import (
"library/database/etcd"
"testing"
)

func TestCaptchaEtcd_Verify(t *testing.T) {
store, err := etcd.New([]string{"10.217.56.146:2379"}, requestTimeout, "", "", 0, 0, "", "", "")
if err != nil {
t.Error("etcd new failed ", err)
return
}
cap := NewClientEtcd(80, 240, store)
id, _, ans, err := cap.GenerateIdAndImage()
if err != nil {
t.Error("captcha generate failed ", err)
return
}
ok, err := cap.Verify(id, ans)
if err != nil {
t.Error("clear false ", err)
return
}
if !ok {
t.Error("verify failed")
return
}

}
```

0 comments on commit b59c8f1

Please sign in to comment.