Merge pull request #4 from Selly-Modules/update-connect-opts

update connect opts - add read pref mode
This commit is contained in:
luuvansinh 2022-03-03 09:30:30 +07:00 committed by GitHub
commit 3e29ed42b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 1 deletions

View File

@ -25,6 +25,7 @@ type ConnectTLSOpts struct {
CaFile string
CertKeyFile string
CertKeyFilePassword string
ReadPreferenceMode string
}
// ConnectStandaloneOpts ...
@ -83,7 +84,7 @@ func connectWithTLS(cfg Config) (*mongo.Database, error) {
pwd := base64DecodeToString(opts.CertKeyFilePassword)
s := "%s/?tls=true&tlsCAFile=./%s&tlsCertificateKeyFile=./%s&tlsCertificateKeyFilePassword=%s&authMechanism=MONGODB-X509"
uri := fmt.Sprintf(s, cfg.Host, caFile.Name(), certFile.Name(), pwd)
readPref := readpref.SecondaryPreferred()
readPref := getReadPref(opts.ReadPreferenceMode)
clientOpts := options.Client().SetReadPreference(readPref).SetReplicaSet(opts.ReplSet).ApplyURI(uri)
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
@ -102,3 +103,15 @@ func connectWithTLS(cfg Config) (*mongo.Database, error) {
func GetInstance() *mongo.Database {
return db
}
func getReadPref(mode string) *readpref.ReadPref {
m, err := readpref.ModeFromString(mode)
if err != nil {
m = readpref.SecondaryPreferredMode
}
readPref, err := readpref.New(m)
if err != nil {
fmt.Println("mongodb.getReadPref err: ", err, m)
}
return readPref
}