Compare commits

..

No commits in common. "master" and "update-connect-opts" have entirely different histories.

5 changed files with 6 additions and 133 deletions

View File

@ -1,42 +0,0 @@
package mongodb
import (
"github.com/thoas/go-funk"
"go.mongodb.org/mongo-driver/bson/primitive"
"sort"
)
// ConvertObjectIDsToStrings ...
func ConvertObjectIDsToStrings(ids []primitive.ObjectID) []string {
return funk.Map(ids, func(item primitive.ObjectID) string {
return item.Hex()
}).([]string)
}
// UniqObjectIds ...
func UniqObjectIds(ids []primitive.ObjectID) []primitive.ObjectID {
idStr := ConvertObjectIDsToStrings(ids)
idStr = funk.UniqString(idStr)
return ConvertStringsToObjectIDs(idStr)
}
// ConvertStringsToObjectIDs ...
func ConvertStringsToObjectIDs(strValues []string) []primitive.ObjectID {
return funk.Map(strValues, func(item string) primitive.ObjectID {
return ConvertStringToObjectID(item)
}).([]primitive.ObjectID)
}
// ConvertStringToObjectID ...
func ConvertStringToObjectID(id string) primitive.ObjectID {
objID, _ := primitive.ObjectIDFromHex(id)
return objID
}
// SortObjectId ...
func SortObjectId(ids []primitive.ObjectID) []primitive.ObjectID {
sort.Slice(ids, func(i, j int) bool {
return ids[i].Timestamp().Before(ids[j].Timestamp())
})
return ids
}

3
go.mod
View File

@ -1,10 +1,9 @@
module git.selly.red/Selly-Modules/mongodb
module github.com/Selly-Modules/mongodb
go 1.16
require (
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/thoas/go-funk v0.9.2 // indirect
go.mongodb.org/mongo-driver v1.7.1
golang.org/x/text v0.3.7
)

4
go.sum
View File

@ -67,11 +67,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thoas/go-funk v0.9.2 h1:oKlNYv0AY5nyf9g+/GhMgS/UO2ces0QRdPKwkhY3VCk=
github.com/thoas/go-funk v0.9.2/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
@ -117,7 +114,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -3,10 +3,8 @@ package mongodb
import (
"context"
"fmt"
"strings"
"github.com/logrusorgru/aurora"
"go.mongodb.org/mongo-driver/event"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
@ -14,9 +12,8 @@ import (
// Config ...
type Config struct {
Host string
DBName string
Monitor *event.CommandMonitor
Host string
DBName string
TLS *ConnectTLSOpts
Standalone *ConnectStandaloneOpts
@ -49,7 +46,7 @@ func Connect(cfg Config) (*mongo.Database, error) {
connectOptions := options.ClientOptions{}
opts := cfg.Standalone
// Set auth if existed
if opts != nil && opts.Username != "" && opts.Password != "" {
if opts.Username != "" && opts.Password != "" {
connectOptions.Auth = &options.Credential{
AuthMechanism: opts.AuthMechanism,
AuthSource: opts.AuthSource,
@ -57,9 +54,6 @@ func Connect(cfg Config) (*mongo.Database, error) {
Password: opts.Password,
}
}
if cfg.Monitor != nil {
connectOptions.SetMonitor(cfg.Monitor)
}
// Connect
client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(cfg.Host))
@ -88,12 +82,10 @@ func connectWithTLS(cfg Config) (*mongo.Database, error) {
return nil, err
}
pwd := base64DecodeToString(opts.CertKeyFilePassword)
uri := getURIWithTLS(cfg, caFile.Name(), certFile.Name(), pwd)
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 := getReadPref(opts.ReadPreferenceMode)
clientOpts := options.Client().SetReadPreference(readPref).SetReplicaSet(opts.ReplSet).ApplyURI(uri)
if cfg.Monitor != nil {
clientOpts.SetMonitor(cfg.Monitor)
}
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
return nil, err
@ -107,22 +99,6 @@ func connectWithTLS(cfg Config) (*mongo.Database, error) {
return db, err
}
func getURIWithTLS(cfg Config, caFilePath, certFilePath, pwd string) string {
host := cfg.Host
if strings.Contains(host, "?") {
host += "&"
} else {
if !strings.HasSuffix(host, "/") {
host += "/?"
} else {
host += "?"
}
}
s := "%stls=true&tlsCAFile=./%s&tlsCertificateKeyFile=./%s&tlsCertificateKeyFilePassword=%s&authMechanism=MONGODB-X509"
uri := fmt.Sprintf(s, host, caFilePath, certFilePath, pwd)
return uri
}
// GetInstance ...
func GetInstance() *mongo.Database {
return db

View File

@ -1,7 +1,6 @@
package mongodb
import (
"fmt"
"testing"
"go.mongodb.org/mongo-driver/mongo"
@ -46,58 +45,3 @@ func Test_connectWithTLS(t *testing.T) {
})
}
}
func Test_getURIWithTLS(t *testing.T) {
type args struct {
cfg Config
caFilePath string
certFilePath string
pwd string
}
ca := "ca.pem"
cert := "cert.pem"
pwd := "1"
tests := []struct {
name string
args args
want string
}{
{
name: "uri no options",
args: args{
cfg: Config{Host: "mongodb://localhost:27017"},
caFilePath: ca,
certFilePath: cert,
pwd: pwd,
},
want: fmt.Sprintf("mongodb://localhost:27017/?tls=true&tlsCAFile=./%s&tlsCertificateKeyFile=./%s&tlsCertificateKeyFilePassword=%s&authMechanism=MONGODB-X509", ca, cert, pwd),
},
{
name: "uri no options, end with /",
args: args{
cfg: Config{Host: "mongodb://localhost:27017/"},
caFilePath: ca,
certFilePath: cert,
pwd: pwd,
},
want: fmt.Sprintf("mongodb://localhost:27017/?tls=true&tlsCAFile=./%s&tlsCertificateKeyFile=./%s&tlsCertificateKeyFilePassword=%s&authMechanism=MONGODB-X509", ca, cert, pwd),
},
{
name: "uri has options",
args: args{
cfg: Config{Host: "mongodb://localhost:27017/?a=1"},
caFilePath: ca,
certFilePath: cert,
pwd: pwd,
},
want: fmt.Sprintf("mongodb://localhost:27017/?a=1&tls=true&tlsCAFile=./%s&tlsCertificateKeyFile=./%s&tlsCertificateKeyFilePassword=%s&authMechanism=MONGODB-X509", ca, cert, pwd),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getURIWithTLS(tt.args.cfg, tt.args.caFilePath, tt.args.certFilePath, tt.args.pwd); got != tt.want {
t.Errorf("getURIWithTLS() = %v, want %v", got, tt.want)
}
})
}
}