/
connection.go
55 lines (42 loc) · 1.08 KB
/
connection.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"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
playerDb = "playerdb"
player = "players"
)
var (
PlayerCollection *mongo.Collection
Gcsb *mongo.Database
)
func InitAuth() {
// err := godotenv.Load()
// if err != nil {
// fmt.Println("Error loading .env")
// return
// }
// DB_URI := os.Getenv("DB")
clientOptions := options.Client().ApplyURI(`mongodb+srv://soubhik:soubhik@sports.vj9j4tb.mongodb.net/?retryWrites=true&w=majority&appName=Sports`)
client, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
panic(err)
}
Gcsb = client.Database(playerDb)
PlayerCollection = Gcsb.Collection(player)
if _, err = PlayerCollection.Indexes().CreateOne(
context.Background(),
mongo.IndexModel{
Keys: bson.D{{Key: "email", Value: 1}},
Options: options.Index().SetUnique(true),
},
); err != nil {
fmt.Println("Failed to Create index ", err.Error())
return
}
fmt.Println("PlayerDB/Player Collection is Ready.")
}