-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
162 lines (125 loc) · 2.98 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package gouse
import (
"context"
"database/sql"
"fmt"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/go-redis/redis/v8"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
/* Connect to Redis */
func ConnectRedis(addr, pass string, dbNo ...int) *redis.Client {
nums := 0
if len(dbNo) > 0 {
nums = dbNo[0]
}
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: pass,
DB: nums,
})
pong, err := client.Ping(client.Context()).Result()
if err != nil {
log.Fatal(err)
}
Println("Connected to Redis: ", pong)
defer client.Close()
return client
}
func ConnectRedisUri(uri string) *redis.Client {
opt, err := redis.ParseURL(uri)
if err != nil {
panic(err)
}
client := redis.NewClient(opt)
pong, err := client.Ping(client.Context()).Result()
if err != nil {
log.Fatal(err)
}
Println("Connected to Redis: ", pong)
defer client.Close()
return client
}
/* Connect to Postgres */
type PgDB struct {
Client *sql.DB
}
func ConnectPostgres(dsn string) (*PgDB, error) {
const maxOpenDbConn = 10
const maxIdleDbConn = 5
const maxDbLifeTime = 5 * time.Minute
dbConn := &PgDB{}
db, err := sql.Open("pgx", dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(maxOpenDbConn)
db.SetMaxIdleConns(maxIdleDbConn)
db.SetConnMaxLifetime(maxDbLifeTime)
err = db.Ping()
if err != nil {
Println("*** Pinged database successfully ***")
log.Fatal(err)
}
dbConn.Client = db
defer func() {
db.Close()
}()
return dbConn, nil
}
/* Connect to MinIO */
type MinioConf struct {
Endpoint string
AccessKey string
SecretKey string
UseSSL bool
Bucket string
Location string
}
func ConnectMinio(ctx context.Context, conf MinioConf) (*minio.Client, error) {
minioClient, errInit := minio.New(conf.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(conf.AccessKey, conf.SecretKey, ""),
Secure: conf.UseSSL,
})
if errInit != nil {
log.Fatalln(errInit)
}
exists, errBucketExists := minioClient.BucketExists(ctx, conf.Bucket)
if errBucketExists == nil && exists {
log.Printf("We already own %s\n", conf.Bucket)
return minioClient, errInit
} else {
err := minioClient.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{
Region: conf.Location,
})
if err != nil {
return minioClient, errInit
} else {
log.Printf("Successfully created %s\n", conf.Bucket)
return minioClient, errInit
}
}
}
/* Connect to MongoDB */
func ConnectMongo(ctx context.Context, uri string) (*mongo.Client, error) {
if uri == "" {
return nil, fmt.Errorf("uri connection is not set")
}
clientOptions := options.Client().ApplyURI(uri)
client, err := mongo.Connect(ctx, clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to connect to MongoDB: %w", err)
}
err = client.Ping(ctx, nil)
if err != nil {
return nil, fmt.Errorf("failed to ping MongoDB: %w", err)
}
defer func() {
client.Disconnect(ctx)
}()
return client, nil
}