merch-api/pkg/dbase/handler.go

39 lines
702 B
Go
Raw Normal View History

2026-02-23 20:22:21 +03:00
package dbase
import (
2026-03-04 17:02:11 +03:00
"context"
2026-02-23 20:22:21 +03:00
"fmt"
2026-03-04 17:02:11 +03:00
_ "github.com/jackc/pgx"
"github.com/jackc/pgx/v5/pgxpool"
2026-02-23 20:22:21 +03:00
)
type Deps struct {
Host string
Port string
Username string
Password string
DBName string
}
2026-03-04 17:02:11 +03:00
func ConnectPool(ctx context.Context, deps Deps) (*pgxpool.Pool, error) {
connString := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s",
2026-02-23 20:22:21 +03:00
deps.Host, deps.Port, deps.Username, deps.Password, deps.DBName)
2026-03-04 17:02:11 +03:00
config, err := pgxpool.ParseConfig(connString)
2026-02-23 20:22:21 +03:00
if err != nil {
return nil, err
}
2026-03-04 17:02:11 +03:00
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return nil, err
}
if err = pool.Ping(ctx); err != nil {
pool.Close()
return nil, err
2026-02-23 20:22:21 +03:00
}
2026-03-04 17:02:11 +03:00
return pool, nil
2026-02-23 20:22:21 +03:00
}