29 lines
612 B
Go
29 lines
612 B
Go
package mongo
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.acooldomain.co/server-manager/backend-kubernetes-go/models"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func Connect(config *models.MongoDBConfig) (*mongo.Client, error) {
|
|
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
|
|
opts := options.Client().ApplyURI(config.Url).SetServerAPIOptions(serverAPI)
|
|
opts.SetAuth(
|
|
options.Credential{
|
|
Username: config.Username,
|
|
Password: config.Password,
|
|
},
|
|
)
|
|
|
|
client, err := mongo.Connect(context.TODO(), opts)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return client, nil
|
|
}
|