mongodb/mongodb.go

123 lines
3.0 KiB
Go
Raw Normal View History

2021-08-09 03:20:27 +00:00
package mongodb
import (
"context"
"fmt"
"github.com/logrusorgru/aurora"
2022-03-04 13:14:30 +00:00
"go.mongodb.org/mongo-driver/event"
2021-08-09 03:20:27 +00:00
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
2022-02-22 08:24:31 +00:00
"go.mongodb.org/mongo-driver/mongo/readpref"
2021-08-09 03:20:27 +00:00
)
2022-02-24 08:00:42 +00:00
// Config ...
type Config struct {
2022-03-04 13:14:30 +00:00
Host string
DBName string
Monitor *event.CommandMonitor
2022-02-24 08:00:42 +00:00
TLS *ConnectTLSOpts
Standalone *ConnectStandaloneOpts
}
2022-02-22 08:24:31 +00:00
// ConnectTLSOpts ...
type ConnectTLSOpts struct {
2022-02-24 08:00:42 +00:00
ReplSet string
CaFile string
CertKeyFile string
CertKeyFilePassword string
ReadPreferenceMode string
2022-02-22 08:24:31 +00:00
}
2022-02-24 08:00:42 +00:00
// ConnectStandaloneOpts ...
type ConnectStandaloneOpts struct {
AuthMechanism string
AuthSource string
Username string
Password string
2022-02-22 08:24:31 +00:00
}
2022-02-24 08:00:42 +00:00
var db *mongo.Database
2021-08-09 03:20:27 +00:00
// Connect to mongo server
2022-02-24 08:00:42 +00:00
func Connect(cfg Config) (*mongo.Database, error) {
if cfg.TLS != nil && cfg.TLS.ReplSet != "" {
return connectWithTLS(cfg)
}
2021-08-09 03:20:27 +00:00
connectOptions := options.ClientOptions{}
2022-02-24 08:00:42 +00:00
opts := cfg.Standalone
2021-08-09 03:20:27 +00:00
// Set auth if existed
2022-02-24 08:00:42 +00:00
if opts.Username != "" && opts.Password != "" {
2021-08-09 03:20:27 +00:00
connectOptions.Auth = &options.Credential{
2022-02-24 08:00:42 +00:00
AuthMechanism: opts.AuthMechanism,
AuthSource: opts.AuthSource,
Username: opts.Username,
Password: opts.Password,
2021-08-09 03:20:27 +00:00
}
}
// Connect
2022-02-24 08:00:42 +00:00
client, err := mongo.Connect(context.Background(), connectOptions.ApplyURI(cfg.Host))
2021-08-09 03:20:27 +00:00
if err != nil {
2022-02-24 08:00:42 +00:00
fmt.Println("Error when connect to MongoDB database", cfg.Host, err)
return nil, err
2021-08-09 03:20:27 +00:00
}
2022-02-24 08:00:42 +00:00
fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + cfg.Host + " --- DB: " + cfg.DBName))
2021-08-09 03:20:27 +00:00
// Set data
2022-02-24 08:00:42 +00:00
db = client.Database(cfg.DBName)
return db, nil
2021-08-09 03:20:27 +00:00
}
2022-02-24 08:00:42 +00:00
func connectWithTLS(cfg Config) (*mongo.Database, error) {
ctx := context.Background()
opts := cfg.TLS
caFile, err := initFileFromBase64String("ca.pem", opts.CaFile)
if err != nil {
return nil, err
}
certFile, err := initFileFromBase64String("cert.pem", opts.CertKeyFile)
if err != nil {
return nil, err
}
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 := getReadPref(opts.ReadPreferenceMode)
2022-02-24 08:00:42 +00:00
clientOpts := options.Client().SetReadPreference(readPref).SetReplicaSet(opts.ReplSet).ApplyURI(uri)
2022-03-04 13:14:30 +00:00
if cfg.Monitor != nil {
clientOpts.SetMonitor(cfg.Monitor)
}
2022-02-24 08:00:42 +00:00
client, err := mongo.Connect(ctx, clientOpts)
if err != nil {
return nil, err
}
if err := client.Ping(ctx, readpref.SecondaryPreferred()); err != nil {
return nil, err
}
db = client.Database(cfg.DBName)
fmt.Println(aurora.Green("*** CONNECTED TO MONGODB: " + cfg.Host + " --- DB: " + cfg.DBName))
return db, err
}
2021-08-09 03:20:27 +00:00
// GetInstance ...
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
}