mirror-nourybot/internal/db/connect.go
2022-03-06 20:19:55 +01:00

47 lines
955 B
Go

package db
import (
"context"
"time"
"github.com/lyx0/nourybot/internal/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
}