mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
39 lines
714 B
Go
39 lines
714 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"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"
|
||
|
)
|
||
|
|
||
|
func Connect() {
|
||
|
conf := config.LoadConfig()
|
||
|
|
||
|
client, err := mongo.NewClient(options.Client().ApplyURI(conf.MongoURI))
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
|
||
|
err = client.Connect(ctx)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
defer client.Disconnect(ctx)
|
||
|
|
||
|
/*
|
||
|
List databases
|
||
|
*/
|
||
|
databases, err := client.ListDatabaseNames(ctx, bson.M{})
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
fmt.Println(databases)
|
||
|
|
||
|
}
|