ALL 0.1.0 Code
This commit is contained in:
50
server/internal/database/database.go
Normal file
50
server/internal/database/database.go
Normal file
@@ -0,0 +1,50 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user