diff --git a/cmd/api/main.go b/cmd/api/main.go index b53bd80..dbd4fe4 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -9,7 +9,7 @@ import ( "log" "time" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/joho/godotenv" "github.com/resend/resend-go/v3" "github.com/stripe/stripe-go/v86" @@ -28,7 +28,7 @@ type config struct { type application struct { config config - db *pgx.Conn + db *pgxpool.Pool resend *resend.Client stripe *stripe.Client cache *cache.RedisCache @@ -53,11 +53,15 @@ func main() { ctx := context.Background() - conn, pgErr := pgx.Connect(ctx, cfg.db.dsn) - if pgErr != nil { - panic(pgErr) + dbpool, poolErr := pgxpool.New(ctx, cfg.db.dsn) + if poolErr != nil { + panic(poolErr) + } + defer dbpool.Close() + + if err := dbpool.Ping(ctx); err != nil { + panic(err) } - defer conn.Close(ctx) log.Println("Database connected successfully") @@ -76,7 +80,7 @@ func main() { app := &application{ config: cfg, - db: conn, + db: dbpool, resend: client, stripe: sc, cache: redisCache, diff --git a/internal/auth/services.go b/internal/auth/services.go index 8454aaa..46551bc 100644 --- a/internal/auth/services.go +++ b/internal/auth/services.go @@ -6,8 +6,8 @@ import ( codeerror "gin-api-1/internal/codeerror" "github.com/google/uuid" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" ) type Service interface { @@ -26,10 +26,10 @@ type authRepository interface { type svc struct { repo authRepository - db *pgx.Conn + db *pgxpool.Pool } -func NewAuthService(repo authRepository, db *pgx.Conn) Service { +func NewAuthService(repo authRepository, db *pgxpool.Pool) Service { return &svc{ repo: repo, db: db, diff --git a/internal/integrations/services.go b/internal/integrations/services.go index 4511ab6..50447d6 100644 --- a/internal/integrations/services.go +++ b/internal/integrations/services.go @@ -16,6 +16,7 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" ) var repositoryPattern = regexp.MustCompile(`^[A-Za-z0-9-_.]+/[A-Za-z0-9-_.]+$`) @@ -30,11 +31,11 @@ type Service interface { type svc struct { repo *repo.Queries - db *pgx.Conn + db *pgxpool.Pool github *gitHubClient } -func NewIntegrationsService(repo *repo.Queries, db *pgx.Conn) Service { +func NewIntegrationsService(repo *repo.Queries, db *pgxpool.Pool) Service { return &svc{ repo: repo, db: db, diff --git a/internal/messages/services.go b/internal/messages/services.go index b3d3bf6..be741c7 100644 --- a/internal/messages/services.go +++ b/internal/messages/services.go @@ -6,8 +6,8 @@ import ( "gin-api-1/internal/codeerror" "github.com/google/uuid" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" ) type Service interface { @@ -23,10 +23,10 @@ type messagesRepository interface { type svc struct { repo messagesRepository - db *pgx.Conn + db *pgxpool.Pool } -func NewMessagesService(repo messagesRepository, db *pgx.Conn) Service { +func NewMessagesService(repo messagesRepository, db *pgxpool.Pool) Service { return &svc{ repo: repo, db: db, diff --git a/internal/projects/services.go b/internal/projects/services.go index 49c49c4..67fe10b 100644 --- a/internal/projects/services.go +++ b/internal/projects/services.go @@ -10,6 +10,7 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" ) type Service interface { @@ -23,10 +24,10 @@ type Service interface { type svc struct { repo *repo.Queries - db *pgx.Conn + db *pgxpool.Pool } -func NewProjectsService(repo *repo.Queries, db *pgx.Conn) Service { +func NewProjectsService(repo *repo.Queries, db *pgxpool.Pool) Service { return &svc{ repo: repo, db: db, diff --git a/internal/subscriptions/services.go b/internal/subscriptions/services.go index 1d7fbe5..72fc07d 100644 --- a/internal/subscriptions/services.go +++ b/internal/subscriptions/services.go @@ -6,8 +6,8 @@ import ( "gin-api-1/internal/codeerror" "github.com/google/uuid" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" "github.com/stripe/stripe-go/v86" ) @@ -21,11 +21,11 @@ type Service interface { type svc struct { repo *repo.Queries - db *pgx.Conn + db *pgxpool.Pool client *stripe.Client } -func NewSubscriptionsService(repo *repo.Queries, db *pgx.Conn, client *stripe.Client) Service { +func NewSubscriptionsService(repo *repo.Queries, db *pgxpool.Pool, client *stripe.Client) Service { return &svc{ repo: repo, db: db, diff --git a/internal/tasks/services.go b/internal/tasks/services.go index 0f026f2..7cb1537 100644 --- a/internal/tasks/services.go +++ b/internal/tasks/services.go @@ -7,8 +7,8 @@ import ( "time" "github.com/google/uuid" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" ) const dateLayout = "2006-01-02" @@ -23,10 +23,10 @@ type Service interface { type svc struct { repo *repo.Queries - db *pgx.Conn + db *pgxpool.Pool } -func NewTasksService(repo *repo.Queries, db *pgx.Conn) Service { +func NewTasksService(repo *repo.Queries, db *pgxpool.Pool) Service { return &svc{ repo: repo, db: db, diff --git a/internal/workspace/services.go b/internal/workspace/services.go index 03506f2..333ad20 100644 --- a/internal/workspace/services.go +++ b/internal/workspace/services.go @@ -11,6 +11,7 @@ import ( "github.com/google/uuid" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" "github.com/stripe/stripe-go/v86" ) @@ -25,11 +26,11 @@ type Service interface { type svc struct { repo *repo.Queries - db *pgx.Conn + db *pgxpool.Pool stripe payment.Svc } -func NewWorkspaceService(repo *repo.Queries, db *pgx.Conn, stripe payment.Svc) Service { +func NewWorkspaceService(repo *repo.Queries, db *pgxpool.Pool, stripe payment.Svc) Service { return &svc{ repo: repo, db: db, diff --git a/internal/workspacemembers/services.go b/internal/workspacemembers/services.go index 04954cb..2c36ed3 100644 --- a/internal/workspacemembers/services.go +++ b/internal/workspacemembers/services.go @@ -6,8 +6,8 @@ import ( codeerror "gin-api-1/internal/codeerror" "github.com/google/uuid" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgtype" + "github.com/jackc/pgx/v5/pgxpool" ) type Service interface { @@ -18,10 +18,10 @@ type Service interface { type svc struct { repo *repo.Queries - db *pgx.Conn + db *pgxpool.Pool } -func NewWorkspaceMembersService(repo *repo.Queries, db *pgx.Conn) Service { +func NewWorkspaceMembersService(repo *repo.Queries, db *pgxpool.Pool) Service { return &svc{ repo: repo, db: db,