/
mongodb.go
executable file
·44 lines (38 loc) · 1.03 KB
/
mongodb.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
package mongodb
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func NewClient(ctx context.Context, host string, port uint16, username, password, database, authDB string) (*mongo.Database, error) {
var (
mongoDBURL string
isAuth bool
)
if username == "" && password == "" {
mongoDBURL = fmt.Sprintf("mongodb://%s:%d", host, port)
} else {
isAuth = true
mongoDBURL = fmt.Sprintf("mongodb://%s:%s@%s:%d", username, password, host, port)
}
clientOptions := options.Client().ApplyURI(mongoDBURL)
if isAuth {
if authDB == "" {
authDB = database
}
clientOptions.SetAuth(options.Credential{
AuthSource: authDB,
Username: username,
Password: password,
})
}
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to connect to mongodb. %v", err)
}
if err = client.Ping(ctx, nil); err != nil {
return nil, fmt.Errorf("Failed to ping mongoDB. %v", err)
}
return client.Database(database), nil
}