|
|
|
@ -58,15 +58,29 @@ func init() {
|
|
|
|
|
// example: mongodb://$user:$password@mongo1.mongo:27017,mongo2.mongo:27017,mongo3.mongo:27017/$DBDatabase/?replicaSet=rs0&readPreference=secondary&authSource=admin&maxPoolSize=$DBMaxPoolSize
|
|
|
|
|
uri = config.Config.Mongo.DBUri
|
|
|
|
|
} else {
|
|
|
|
|
//mongodb://mongodb1.example.com:27317,mongodb2.example.com:27017/?replicaSet=mySet&authSource=authDB
|
|
|
|
|
mongodbHosts := ""
|
|
|
|
|
for i, v := range config.Config.Mongo.DBAddress {
|
|
|
|
|
if i == len(config.Config.Mongo.DBAddress)-1 {
|
|
|
|
|
mongodbHosts += v
|
|
|
|
|
} else {
|
|
|
|
|
mongodbHosts += v + ","
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if config.Config.Mongo.DBPassword != "" && config.Config.Mongo.DBUserName != "" {
|
|
|
|
|
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin", config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, config.Config.Mongo.DBAddress,
|
|
|
|
|
// clientOpts := options.Client().ApplyURI("mongodb://localhost:27017,localhost:27018/?replicaSet=replset")
|
|
|
|
|
//mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
|
|
|
|
|
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin&replicaSet=replset",
|
|
|
|
|
config.Config.Mongo.DBUserName, config.Config.Mongo.DBPassword, mongodbHosts,
|
|
|
|
|
config.Config.Mongo.DBDatabase, config.Config.Mongo.DBMaxPoolSize)
|
|
|
|
|
} else {
|
|
|
|
|
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
|
|
|
|
config.Config.Mongo.DBAddress, config.Config.Mongo.DBDatabase,
|
|
|
|
|
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin&replicaSet=replset",
|
|
|
|
|
mongodbHosts, config.Config.Mongo.DBDatabase,
|
|
|
|
|
config.Config.Mongo.DBMaxPoolSize)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mongoClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(" mongo.Connect failed, try ", utils.GetSelfFuncName(), err.Error(), uri)
|
|
|
|
|