initial commit
This commit is contained in:
commit
88c14b627e
224 files changed
+17204
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lincodex/backend/config"
|
||||
"lincodex/backend/internal/adapter/secondary/postgres/sqlc"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
type HealthChecker struct {
|
||||
pool *pgxpool.Pool
|
||||
queries *sqlc.Queries
|
||||
}
|
||||
|
||||
func NewHealthChecker(ctx context.Context, config config.Config) (*HealthChecker, func(), error) {
|
||||
if config.DatabaseURL == "" {
|
||||
return &HealthChecker{}, func() {}, nil
|
||||
}
|
||||
|
||||
poolConfig, err := pgxpool.ParseConfig(config.DatabaseURL)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("parse DATABASE_URL: %w", err)
|
||||
}
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("create PostgreSQL pool: %w", err)
|
||||
}
|
||||
|
||||
return &HealthChecker{pool: pool, queries: sqlc.New(pool)}, pool.Close, nil
|
||||
}
|
||||
|
||||
func (checker *HealthChecker) Check(ctx context.Context) error {
|
||||
if checker.pool == nil || checker.queries == nil {
|
||||
return outbound.ErrNotReady
|
||||
}
|
||||
if _, err := checker.queries.Ping(ctx); err != nil {
|
||||
return fmt.Errorf("PostgreSQL health query: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user