first commit of restructured project

This commit is contained in:
KS Jannette
2026-02-22 15:21:18 -05:00
commit 9fca234606
75 changed files with 8299 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
package db
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func Connect(dsn string) (*pgxpool.Pool, error) {
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, fmt.Errorf("parse dsn: %w", err)
}
cfg.MaxConns = 20
cfg.MinConns = 2
cfg.MaxConnIdleTime = 30 * time.Second
cfg.MaxConnLifetime = 5 * time.Minute
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
p, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("create pool: %w", err)
}
if err := p.Ping(ctx); err != nil {
p.Close()
return nil, fmt.Errorf("ping: %w", err)
}
return p, nil
}
func TestConnection(p *pgxpool.Pool) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var now time.Time
err := p.QueryRow(ctx, "SELECT NOW()").Scan(&now)
if err != nil {
return fmt.Errorf("test query: %w", err)
}
fmt.Printf("[DB] Connection successful at %s\n", now.Format(time.RFC3339))
return nil
}