-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_config.go
95 lines (78 loc) · 1.91 KB
/
mongo_config.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ginboot
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoConfig struct {
Host string
Port int
Username string
Password string
Database string
Options map[string]string
}
func NewMongoConfig() *MongoConfig {
return &MongoConfig{
Host: "localhost",
Port: 27017,
Options: make(map[string]string),
}
}
func (c *MongoConfig) WithCredentials(username, password string) *MongoConfig {
c.Username = username
c.Password = password
return c
}
func (c *MongoConfig) WithHost(host string, port int) *MongoConfig {
c.Host = host
c.Port = port
return c
}
func (c *MongoConfig) WithDatabase(database string) *MongoConfig {
c.Database = database
return c
}
func (c *MongoConfig) WithOption(key, value string) *MongoConfig {
c.Options[key] = value
return c
}
func (c *MongoConfig) BuildURI() string {
var auth string
if c.Username != "" && c.Password != "" {
auth = fmt.Sprintf("%s:%s@", c.Username, c.Password)
}
uri := fmt.Sprintf("mongodb://%s%s:%d", auth, c.Host, c.Port)
if len(c.Options) > 0 {
uri += "?"
first := true
for key, value := range c.Options {
if !first {
uri += "&"
}
uri += fmt.Sprintf("%s=%s", key, value)
first = false
}
}
return uri
}
func (c *MongoConfig) Connect() (*mongo.Database, error) {
uri := c.BuildURI()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
clientOptions := options.Client().
ApplyURI(uri).
SetServerSelectionTimeout(10 * time.Second).
SetConnectTimeout(10 * time.Second)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to create MongoDB client: %v", err)
}
// Ping the database to verify connection
if err = client.Ping(ctx, nil); err != nil {
return nil, fmt.Errorf("failed to ping MongoDB: %v", err)
}
return client.Database(c.Database), nil
}