mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
50 lines
959 B
Go
50 lines
959 B
Go
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"
|
|
)
|
|
|
|
type Channel struct {
|
|
Name string `bson:"name,omitempty"`
|
|
Connect bool `bson:"connect,omitempty"`
|
|
}
|
|
|
|
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
|
|
}
|