-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo.go
71 lines (56 loc) · 1.69 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
70
71
package mongodb
import (
"context"
"git.golaxy.org/core/service"
"git.golaxy.org/core/utils/option"
"git.golaxy.org/framework/plugins/db"
"git.golaxy.org/framework/plugins/log"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
type IMongoDB interface {
MongoDB(tag string) *mongo.Client
}
func newMongoDB(settings ...option.Setting[MongoDBOptions]) IMongoDB {
return &_MongoDB{
options: option.Make(With.Default(), settings...),
dbs: make(map[string]*mongo.Client),
}
}
type _MongoDB struct {
servCtx service.Context
options MongoDBOptions
dbs map[string]*mongo.Client
}
func (m *_MongoDB) InitSP(ctx service.Context) {
log.Infof(ctx, "init plugin %q", self.Name)
m.servCtx = ctx
for _, info := range m.options.DBInfos {
m.dbs[info.Tag] = m.connectToDB(info)
}
}
func (m *_MongoDB) ShutSP(ctx service.Context) {
log.Infof(ctx, "shut plugin %q", self.Name)
for _, db := range m.dbs {
db.Disconnect(context.Background())
}
}
func (m *_MongoDB) MongoDB(tag string) *mongo.Client {
return m.dbs[tag]
}
func (m *_MongoDB) connectToDB(info db.DBInfo) *mongo.Client {
opt := options.Client().ApplyURI(info.ConnStr)
client, err := mongo.NewClient(opt)
if err != nil {
log.Panicf(m.servCtx, "conn to db %q failed, %s", info.ConnStr, err)
}
if err := client.Connect(context.Background()); err != nil {
log.Panicf(m.servCtx, "conn to db %q failed, %s", info.ConnStr, err)
}
if err := client.Ping(context.Background(), readpref.Primary()); err != nil {
log.Panicf(m.servCtx, "ping db %q failed, %s", info.ConnStr, err)
}
log.Infof(m.servCtx, "conn to db %q ok", info.ConnStr)
return client
}