/
mongo.go
43 lines (33 loc) · 998 Bytes
/
mongo.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
// Handle the Mongo DB connection
package db
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
)
const (
DB_TIMEOUT = 20 * time.Second
)
func InitDB(dbUri string) *mongo.Database {
connectionString, errParse := connstring.ParseAndValidate(dbUri)
if errParse != nil {
log.Fatalf("Bad DB connection string: %v", dbUri)
}
ctx, cancel := context.WithTimeout(context.Background(), DB_TIMEOUT)
defer cancel()
client, errMongo := mongo.Connect(ctx, options.Client().ApplyURI(dbUri))
if errMongo != nil {
log.Fatalf("Could not connect to DB: %v", dbUri)
}
return client.Database(connectionString.Database)
}
func CloseDB(ctx context.Context, db *mongo.Database) {
ctx, cancel := context.WithTimeout(ctx, DB_TIMEOUT)
defer cancel()
if err := db.Client().Disconnect(ctx); err != nil {
log.Printf("Error while closing DB connection. Error: %+v", err)
}
}