change code for new version

This commit is contained in:
namhq1989 2022-08-24 15:46:56 +07:00
parent 2e42b0a20a
commit 8abf9ac5ed
1 changed files with 3 additions and 11 deletions

View File

@ -23,21 +23,19 @@ type Config struct {
ConnectionLifetime time.Duration ConnectionLifetime time.Duration
} }
var db *sql.DB
// Connect ... // Connect ...
func Connect(cfg Config, server string) { func Connect(cfg Config, server string) *sql.DB {
uri := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s", uri := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=%s",
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode) cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, cfg.SSLMode)
// connect // connect
c, err := sql.Open("pgx", uri) db, err := sql.Open("pgx", uri)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// ping // ping
if err = c.Ping(); err != nil { if err = db.Ping(); err != nil {
logger.Error("pgx ping", logger.LogData{ logger.Error("pgx ping", logger.LogData{
Source: "Connect", Source: "Connect",
Message: err.Error(), Message: err.Error(),
@ -46,9 +44,6 @@ func Connect(cfg Config, server string) {
panic(err) panic(err)
} }
// assign
db = c
// config // config
if cfg.MaxOpenConnections == 0 { if cfg.MaxOpenConnections == 0 {
cfg.MaxOpenConnections = 25 cfg.MaxOpenConnections = 25
@ -70,9 +65,6 @@ func Connect(cfg Config, server string) {
boil.DebugMode = cfg.IsDebug boil.DebugMode = cfg.IsDebug
fmt.Printf("⚡️[postgres]: connected to %s:%d \n", cfg.Host, cfg.Port) fmt.Printf("⚡️[postgres]: connected to %s:%d \n", cfg.Host, cfg.Port)
}
// GetDB ...
func GetDB() *sql.DB {
return db return db
} }