-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo.go
145 lines (112 loc) · 4.79 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// MIT License
// Copyright (c) The RAI Authors
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package dbdrivers
import (
"context"
"encoding/json"
"time"
"github.com/retail-ai-inc/bean/aes"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gorm.io/gorm"
)
type MongoConfig struct {
Master *struct {
Database string
Username string
Password string
Host string
Port string
}
ConnectTimeout time.Duration
MaxConnectionPoolSize uint64
MaxConnectionLifeTime time.Duration
}
// Init the mongo database connection map.
func InitMongoTenantConns(config MongoConfig, master *gorm.DB, tenantAlterDbHostParam, tenantDBPassPhraseKey string) (map[uint64]*mongo.Client, map[uint64]string) {
tenantCfgs := GetAllTenantCfgs(master)
return getAllMongoTenantDB(config, tenantCfgs, tenantAlterDbHostParam, tenantDBPassPhraseKey)
}
func InitMongoMasterConn(config MongoConfig) (*mongo.Client, string) {
masterCfg := config.Master
if masterCfg != nil && masterCfg.Database != "" {
return connectMongoDB(masterCfg.Username, masterCfg.Password, masterCfg.Host, masterCfg.Port, masterCfg.Database,
config.MaxConnectionPoolSize, config.ConnectTimeout, config.MaxConnectionLifeTime)
}
return nil, ""
}
func getAllMongoTenantDB(config MongoConfig, tenantCfgs []*TenantConnections, tenantAlterDbHostParam, tenantDBPassPhraseKey string) (map[uint64]*mongo.Client, map[uint64]string) {
mongoConns := make(map[uint64]*mongo.Client, len(tenantCfgs))
mongoDBNames := make(map[uint64]string, len(tenantCfgs))
for _, t := range tenantCfgs {
var cfgsMap map[string]map[string]interface{}
var err error
if t.Connections != nil {
if err = json.Unmarshal(t.Connections, &cfgsMap); err != nil {
panic(err)
}
}
// IMPORTANT: Check the `mongodb` object exist in the Connections column or not.
if mongoCfg, ok := cfgsMap["mongodb"]; ok {
userName := mongoCfg["username"].(string)
password := mongoCfg["password"].(string)
// IMPORTANT: If tenant database password is encrypted in master db config.
if tenantDBPassPhraseKey != "" {
password, err = aes.BeanAESDecrypt(tenantDBPassPhraseKey, password)
if err != nil {
panic(err)
}
}
host := mongoCfg["host"].(string)
// IMPORTANT - If a command or service wants to use a different `host` parameter for tenant database connection
// then it's easy to do just by passing that parameter string name using `bean.TenantAlterDbHostParam`.
// Therfore, `bean` will overwrite all host string in `TenantConnections`.`Connections` JSON.
if tenantAlterDbHostParam != "" && mongoCfg[tenantAlterDbHostParam] != nil {
host = mongoCfg[tenantAlterDbHostParam].(string)
}
port := mongoCfg["port"].(string)
dbName := mongoCfg["database"].(string)
mongoConns[t.TenantID], mongoDBNames[t.TenantID] = connectMongoDB(
userName, password, host, port, dbName, config.MaxConnectionPoolSize,
config.ConnectTimeout, config.MaxConnectionLifeTime)
} else {
mongoConns[t.TenantID], mongoDBNames[t.TenantID] = nil, ""
}
}
return mongoConns, mongoDBNames
}
func connectMongoDB(userName, password, host, port, dbName string, maxConnectionPoolSize uint64,
connectTimeout, maxConnectionLifeTime time.Duration) (*mongo.Client, string) {
connStr := "mongodb://" + host + ":" + port
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
opts := options.Client().
ApplyURI(connStr).
SetConnectTimeout(connectTimeout).
SetMaxPoolSize(maxConnectionPoolSize).
SetMaxConnIdleTime(maxConnectionLifeTime)
if userName != "" && password != "" {
credential := options.Credential{Username: userName, Password: password, AuthSource: dbName}
opts.SetAuth(credential)
}
mdb, err := mongo.Connect(ctx, opts)
if err != nil {
panic(err)
}
return mdb, dbName
}