-
Notifications
You must be signed in to change notification settings - Fork 9
/
connection_mongodb.go
48 lines (43 loc) · 1.11 KB
/
connection_mongodb.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
package goutils
import (
"context"
_ "github.com/go-sql-driver/mysql"
_ "github.com/joho/godotenv/autoload"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"time"
)
func ConnectionMongoDB() *mongo.Database {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var max uint64
max = 10
clientOptions := options.ClientOptions{MaxPoolSize: &max}
user := Godotenv("mongo_user")
if user == "" {
user = Godotenv("logger_user")
}
password := Godotenv("mongo_password")
if password == "" {
password = Godotenv("logger_password")
}
url := Godotenv("mongo_url")
if url == "" {
url = Godotenv("logger_url")
}
database := Godotenv("mongo_database")
if database == "" {
database = Godotenv("logger_database")
}
Godotenv("logger_password")
Godotenv("logger_url")
Godotenv("logger_database")
clientOptions.SetAuth(options.Credential{Username: user, Password: password})
clientOptions.ApplyURI(url)
client, err := mongo.Connect(ctx, &clientOptions)
if err != nil {
log.Fatalln(err.Error())
}
return client.Database(database)
}