-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptcha.go
47 lines (36 loc) · 1.13 KB
/
captcha.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
package captcha
import (
"fmt"
"github.com/webx-top/com"
)
var drivers = map[string]Constructor{}
func Register(name string, constructor Constructor) {
drivers[name] = constructor
}
func Open(name string, cType string, store Storer, options ...Option) (Driver, error) {
constructor, ok := drivers[name]
if !ok {
return nil, ErrUnsupported
}
return constructor(cType, store, options...)
}
func Unregister(name string) {
delete(drivers, name)
}
var instances = com.InitSafeMap[string, Driver]()
func RegisterInstance(driverName string, captchaType string, instance Driver) {
instances.Set(driverName+`.`+captchaType, instance)
}
func UnregisterInstance(driverName string, captchaType string) {
instances.Delete(driverName + `.` + captchaType)
}
func GetInstance(driverName string, captchaType string) (Driver, error) {
instance, ok := instances.GetOk(driverName + `.` + captchaType)
if !ok {
return nil, fmt.Errorf(`%w: %s`, ErrUnsupported, driverName+`.`+captchaType)
}
return instance, nil
}
func GetInstanceOk(driverName string, captchaType string) (Driver, bool) {
return instances.GetOk(driverName + `.` + captchaType)
}