-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.go
69 lines (61 loc) · 1.33 KB
/
mongo.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
package mongo
import (
"context"
"log"
"github.com/starfork/stargo/store"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Mongo struct {
client *mongo.Client
c *store.Config
ctx context.Context
}
func NewMongo(config *store.Config) store.Store {
return &Mongo{
c: config,
ctx: context.Background(),
}
}
func (e *Mongo) Connect(conf ...*store.Config) {
c := e.c
auth := options.Credential{
//AuthSource: "<authenticationDb>",
Username: c.User,
Password: c.Auth,
}
var client *mongo.Client
clientOptions := options.Client().ApplyURI("mongodb://" + c.Host).SetAuth(auth)
//debug
if c.Monitor {
cmdMonitor := &event.CommandMonitor{
Started: func(_ context.Context, evt *event.CommandStartedEvent) {
//log.Println(evt.Command.String())
//log.Println(evt.CommandName)
},
}
clientOptions.SetMonitor(cmdMonitor)
}
var err error
client, err = mongo.Connect(e.ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
// 检查连接
err = client.Ping(e.ctx, nil)
if err != nil {
log.Fatal(err)
}
e.client = client
}
func (e *Mongo) GetInstance(conf ...*store.Config) *mongo.Client {
if len(conf) > 0 {
e.Connect(conf...)
return e.client
}
return e.client
}
func (e *Mongo) Close() {
e.client.Disconnect(e.ctx)
}