-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
108 lines (92 loc) · 3.18 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
package repositorysdk
import (
"fmt"
"github.com/go-redis/redis/v8"
"gorm.io/driver/postgres"
"gorm.io/gorm"
gormLogger "gorm.io/gorm/logger"
)
// PostgresDatabaseConfig is a struct that holds the configuration details required to establish a connection
// with a PostgreSQL database.
type PostgresDatabaseConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
User string `mapstructure:"username"`
Password string `mapstructure:"password"`
Name string `mapstructure:"name"`
SSL string `mapstructure:"ssl"`
MaxIdleConn int `mapstructure:"max_idle_conn"`
MaxOpenConn int `mapstructure:"max_open_conn"`
}
// GetMaxIdleConn returns the maximum number of idle connections in the connection pool.
// If the value is not set, the default value of 10 is returned.
//
// Returns:
// - int: the maximum number of idle connections.
func (c *PostgresDatabaseConfig) GetMaxIdleConn() int {
if c.MaxIdleConn == 0 {
return 10
}
return c.MaxIdleConn
}
// GetMaxOpenConn returns the maximum number of open connections in the connection pool.
// If the value is not set, the default value of 10 is returned.
//
// Returns:
// - int: the maximum number of open connections.
func (c *PostgresDatabaseConfig) GetMaxOpenConn() int {
if c.MaxOpenConn == 0 {
return 10
}
return c.MaxOpenConn
}
// InitPostgresDatabase initializes a connection to a PostgreSQL database using the given configuration details.
//
// Parameters:
// - conf: a pointer to a PostgresDatabaseConfig struct containing the database configuration details.
// - isDebug: a boolean value to enable or disable the GORM logging mode.
//
// Returns:
// - *gorm.DB: a pointer to the GORM database object.
// - error: an error if something goes wrong, otherwise nil.
func InitPostgresDatabase(conf *PostgresDatabaseConfig, isDebug bool) (*gorm.DB, error) {
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", conf.Host, conf.Port, conf.User, conf.Password, conf.Name, conf.SSL)
gormConf := &gorm.Config{}
if !isDebug {
gormConf.Logger = gormLogger.Default.LogMode(gormLogger.Silent)
}
db, err := gorm.Open(postgres.Open(dsn), gormConf)
if err != nil {
return nil, err
}
sqlDB, err := db.DB()
if err != nil {
return nil, err
}
sqlDB.SetMaxIdleConns(conf.GetMaxIdleConn())
sqlDB.SetMaxOpenConns(conf.GetMaxOpenConn())
return db, nil
}
// RedisConfig is a struct that holds the configuration details required to establish a connection
// with a Redis database.
type RedisConfig struct {
Host string `mapstructure:"host"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
// InitRedisConnect initializes a connection to a Redis database using the given configuration details.
//
// Parameters:
// - conf: a pointer to a RedisConfig struct containing the database configuration details.
//
// Returns:
// - *redis.Client: a pointer to the Redis client object.
// - error: an error if something goes wrong, otherwise nil.
func InitRedisConnect(conf *RedisConfig) (cache *redis.Client, err error) {
cache = redis.NewClient(&redis.Options{
Addr: conf.Host,
Password: conf.Password,
DB: conf.DB,
})
return
}