-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
77 lines (65 loc) · 1.2 KB
/
options.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
75
76
77
package mongobuilder
import "fmt"
type Option func(opts *Options)
type Options struct {
User string
Pass string
Host string
Port int
PoolSize uint64
DBName string
}
func WithUser(user string) Option {
return func(opts *Options) {
opts.User = user
}
}
func WithPass(pass string) Option {
return func(opts *Options) {
opts.Pass = pass
}
}
func WithHost(host string) Option {
return func(opts *Options) {
opts.Host = host
}
}
func WithPort(port int) Option {
return func(opts *Options) {
opts.Port = port
}
}
func WithPoolSize(size uint64) Option {
return func(opts *Options) {
opts.PoolSize = size
}
}
func WithDBName(dbName string) Option {
return func(opts *Options) {
opts.DBName = dbName
}
}
func initOptions(opts ...Option) *Options {
options := &Options{
User: "mongo",
Host: "127.0.0.1",
Port: 27017,
PoolSize: 10,
}
for _, opt := range opts {
opt(options)
}
return options
}
// BuildDBDriver return a driver like "mongodb://user:pass@host:port/dbName"
func BuildDBDriver(opts *Options) string {
driver := fmt.Sprintf(
"mongodb://%s:%s@%s:%d/%s",
opts.User,
opts.Pass,
opts.Host,
opts.Port,
opts.DBName,
)
return driver
}