-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
63 lines (48 loc) · 1.41 KB
/
helpers.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
56
57
58
59
60
61
62
63
package gomongoauth
import (
"context"
"log"
"time"
"github.com/go-playground/validator/v10"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func InitializeDbConnection(uri string) (*mongo.Client, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))
if err != nil {
log.Println("Error connecting to db. Err: ", err.Error())
return client, err
}
// Ping the primary
if err := client.Ping(ctx, readpref.Primary()); err != nil {
log.Println("Unable to ping to primary. Err: ", err.Error())
return client, err
}
log.Println("Connected to db.")
return client, nil
}
// Must be used as a defer function to disconnect from db.
func DisconnectDb(client *mongo.Client) error {
if err := client.Disconnect(context.TODO()); err != nil {
return err
}
log.Println("Disconnecting from db.")
return nil
}
func validateInput(user User) error {
validate := validator.New()
err := validate.Struct(user)
if err != nil {
log.Printf("Error validating user input.")
}
return err
}
func getUserByEmail(collection mongo.Collection, ctx context.Context, email string) (User, error) {
var dbuser User
err := collection.FindOne(ctx, bson.M{"email": email}).Decode(&dbuser)
return dbuser, err
}