-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathsetup.go
More file actions
104 lines (82 loc) · 2.54 KB
/
setup.go
File metadata and controls
104 lines (82 loc) · 2.54 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package configs
import (
"context"
"os"
"strconv"
"sync"
"time"
"github.com/UTDNebula/nebula-api/api/common/log"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type DBSingleton struct {
client *mongo.Client
}
var dbInstance *DBSingleton
var once sync.Once
func ConnectDB() *mongo.Client {
once.Do(func() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(GetEnvMongoURI()))
if err != nil {
log.WriteErrorMsg("Unable to create MongoDB client")
os.Exit(1)
}
defer cancel()
//ping the database
err = client.Ping(ctx, nil)
if err != nil {
log.WriteErrorMsg("Unable to ping database")
os.Exit(1)
}
log.WriteDebug("Connected to MongoDB")
dbInstance = &DBSingleton{
client: client,
}
})
return dbInstance.client
}
// getting database collections
func GetCollection(collectionName string) *mongo.Collection {
client := ConnectDB()
collection := client.Database("combinedDB").Collection(collectionName)
return collection
}
// Returns *options.FindOptions with a limit and offset applied. Returns error if any
func GetOptionLimit(query *bson.M, c *gin.Context) (*options.FindOptions, error) {
delete(*query, "offset") // removes offset (if present) in query --offset is not field in collections
// parses offset if included in the query
var offset int64
var err error
var limit int64 = GetEnvLimit()
if c.Query("offset") == "" {
offset = 0 // default value for offset
} else {
offset, err = strconv.ParseInt(c.Query("offset"), 10, 64)
if err != nil {
return options.Find().SetSkip(0).SetLimit(limit), err // default value for offset
}
}
return options.Find().SetSkip(offset).SetLimit(limit), err
}
// TODO: Is there a chance we can combine this with GetOptionLimit to reduce repretiveness ?
// Returns pairs of the offset and limit for pagination stage for aggregate endpoints pipeline
// returns (offset, limit, err)
func GetAggregateLimit(query *bson.M, c *gin.Context) (int64, int64, error) {
delete(*query, "offset") // remove offset field (if present) in the query
// parses offset if included in the query
var limit int64 = GetEnvLimit()
var offset int64
var err error
if c.Query("offset") == "" {
offset = 0 // default value
} else {
offset, err = strconv.ParseInt(c.Query("offset"), 10, 64)
if err != nil {
return offset, limit, err // default value
}
}
return offset, limit, err
}