/
db.go
55 lines (41 loc) · 1.25 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package db
import (
"context"
"log"
"os"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
// Connect to the DB
var client = connect()
// connect to the database
func connect() *mongo.Client {
log.Println("Connecting to MongoDB...")
// Get the MongoDB URI from the environment
mongoURI := os.Getenv("MONGODB_URI")
// If the URI is empty, use the default
if mongoURI == "" {
mongoURI = "mongodb://localhost:27017"
}
// Use the SetServerAPIOptions() method to set the Stable API version to 1
serverAPI := options.ServerAPI(options.ServerAPIVersion1)
// Set client options
clientOptions := options.Client().ApplyURI(mongoURI).SetServerAPIOptions(serverAPI)
// Connect to MongoDB
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
log.Fatalf("Unable to connect to MongoDB: %v", err)
}
// defer func() {
// if err = client.Disconnect(context.TODO()); err != nil {
// utils.ErrorHandler(err)
// }
// }()
// Send a ping to confirm a successful connection
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
log.Fatalf("Unable to ping MongoDB: %v", err)
}
log.Println("Connected to MongoDB!")
return client
}