51 lines
947 B
Go
51 lines
947 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var DB *mongo.Database
|
|
var Client *mongo.Client
|
|
|
|
func Connect() {
|
|
uri := os.Getenv("MONGO_URI")
|
|
if uri == "" {
|
|
uri = "mongodb://localhost:27017"
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
clientOptions := options.Client().ApplyURI(uri)
|
|
client, err := mongo.Connect(ctx, clientOptions)
|
|
if err != nil {
|
|
log.Fatal("Failed to connect to MongoDB: ", err)
|
|
}
|
|
|
|
err = client.Ping(ctx, nil)
|
|
if err != nil {
|
|
log.Fatal("Failed to ping MongoDB: ", err)
|
|
}
|
|
|
|
fmt.Println("Successfully connected to MongoDB!")
|
|
|
|
Client = client
|
|
|
|
dbName := os.Getenv("MONGO_DB_NAME")
|
|
if dbName == "" {
|
|
dbName = "fpmb"
|
|
}
|
|
DB = client.Database(dbName)
|
|
}
|
|
|
|
func GetCollection(collectionName string) *mongo.Collection {
|
|
return DB.Collection(collectionName)
|
|
}
|