1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
Annika Hannig f9584e711a use config
2022-01-12 15:31:02 +01:00

32 lines
676 B
Go

package postgres
import (
"context"
"os"
"path/filepath"
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/jackc/pgx/v4/pgxpool"
)
// Connect creates and configures a pgx pool
func Connect(ctx context.Context, opts *config.PostgresConfig) (*pgxpool.Pool, error) {
// Initialize postgres connection
cfg, err := pgxpool.ParseConfig(opts.URL)
if err != nil {
return nil, err
}
cfg.ConnConfig.RuntimeParams["application_name"] = filepath.Base(os.Args[0])
if opts.MaxConns == 0 {
return nil, ErrMaxConnsUnconfigured
}
// We need some more connections
cfg.MaxConns = opts.MaxConns
cfg.MinConns = opts.MinConns
return pgxpool.ConnectConfig(ctx, cfg)
}