This repository has been archived by the owner on Dec 11, 2022. It is now read-only.
/
client.go
79 lines (71 loc) · 1.84 KB
/
client.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
package mongodb
import (
"context"
"fmt"
"sync"
"github.com/sirupsen/logrus"
"github.com/vietnam-immigrations/go-utils/pkg/aws/ssm"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
initClient sync.Once
client *mongo.Client
db string
)
func newClient(ctx context.Context, log *logrus.Entry, stage string) (*mongo.Client, error) {
initClient.Do(func() {
log.Infof("init mongodb client")
mongoHost, err := ssm.GetParameter(ctx, log, "vs2", "all", "/mongo/host", false)
if err != nil {
return
}
mongoUsername, err := ssm.GetParameter(ctx, log, "vs2", stage, "/mongo/username", false)
if err != nil {
return
}
mongoPassword, err := ssm.GetParameter(ctx, log, "vs2", stage, "/mongo/password", true)
if err != nil {
return
}
mongoDatabase, err := ssm.GetParameter(ctx, log, "vs2", stage, "/mongo/db", false)
if err != nil {
return
}
log.Infof("host = %s, user = %s, db = %s", mongoHost, mongoUsername, mongoDatabase)
mongoFullUrl := fmt.Sprintf("mongodb+srv://%s", mongoHost)
c, err := mongo.NewClient(
options.Client().ApplyURI(mongoFullUrl).SetAuth(options.Credential{
Username: mongoUsername,
Password: mongoPassword,
}),
)
if err != nil {
log.Errorf("failed to create mongodb client: %s", err)
return
}
err = c.Connect(ctx)
if err != nil {
log.Errorf("failed to connect to mongodb: %s", err)
return
}
client = c
db = mongoDatabase
})
if client == nil {
return nil, fmt.Errorf("failed to init mongodb client")
}
return client, nil
}
func Disconnect(ctx context.Context, log *logrus.Entry) {
if client != nil {
err := client.Disconnect(ctx)
if err != nil {
log.Errorf("failed to disconnect mongodb client: %s", err)
} else {
log.Infof("mongodb client disconnected")
client = nil
initClient = sync.Once{}
}
}
}