package db import ( "context" "time" "github.com/lyx0/nourybot/pkg/config" log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) // Connect connects the the MongoDB database through a supplied cfg // and returns a *mongo.Client func Connect(cfg *config.Config) *mongo.Client { client, err := mongo.NewClient(options.Client().ApplyURI(cfg.MongoURI)) if err != nil { log.Fatal(err) } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err = client.Connect(ctx) if err != nil { log.Fatal(err) } // defer client.Disconnect(ctx) err = client.Ping(context.TODO(), nil) if err != nil { log.Fatal(err) } log.Info("Connected to MongoDB!") databases, err := client.ListDatabaseNames(ctx, bson.M{}) if err != nil { log.Fatal(err) } _ = databases // log.Info(databases) return client }