-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.go
74 lines (60 loc) · 1.5 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package captcha
import (
"context"
)
type Constructor func(captchaType string, store Storer, options ...Option) (Driver, error)
type Driver interface {
Init() error
OptionSetter
MakeData(ctx context.Context) (*Data, error)
Verify(ctx context.Context, key string, response string) error
Storer() Storer
}
type OptionSetter interface {
SetOption(key string, value interface{})
}
type Storer interface {
Put(ctx context.Context, key string, val interface{}, timeout int64) error
Get(ctx context.Context, key string, value interface{}) error
Delete(ctx context.Context, key string) error
}
type APIResponse struct {
Code int `json:"code"`
Message string `json:"message"`
*Data `json:",omitempty"`
}
func (a *APIResponse) SetSuccess(message ...string) *APIResponse {
a.Code = 0
if len(message) > 0 {
a.Message = message[0]
}
return a
}
func (a *APIResponse) SetError(message string) *APIResponse {
a.Code = 1
a.Message = message
return a
}
func (a *APIResponse) SetData(data *Data) *APIResponse {
a.Code = 0
a.Data = data
return a
}
func (a *APIResponse) IsSuccess() bool {
return a.Code == 1
}
type Data struct {
Key string `json:"key"`
Image string `json:"image,omitempty"`
Thumb string `json:"thumb,omitempty"`
Tile *Tile `json:"tile,omitempty"`
}
type Tile struct {
Image string `json:"image"`
Width int `json:"width"`
Height int `json:"height"`
OffsetX int `json:"x"`
OffsetY int `json:"y"`
}
type Option func(d OptionSetter)
const MaxAge = 300 //seconds