38 lines
638 B
Go
38 lines
638 B
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type Config struct {
|
|
URL string `envconfig:"DB__URL" required:"true"`
|
|
}
|
|
|
|
type Pool struct {
|
|
*pgxpool.Pool
|
|
}
|
|
|
|
func New(ctx context.Context, c Config) (*Pool, error) {
|
|
cfg, err := pgxpool.ParseConfig(c.URL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pgxpool.ParseConfig: %w", err)
|
|
}
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pgxpool.NewWithConfig: %w", err)
|
|
}
|
|
|
|
return &Pool{Pool: pool}, nil
|
|
}
|
|
|
|
func (p *Pool) Close() {
|
|
p.Pool.Close()
|
|
|
|
log.Info().Msg("Postgres closed")
|
|
}
|