mirror of
https://github.com/luxfi/broker.git
synced 2026-09-24 00:25:18 +00:00
Same issue as snapshots — Alpaca sandbox returns 404 for
/v1beta3/crypto/us/{sym}/bars but works with
/v1beta3/crypto/us/bars?symbols={sym}. Also handle the wrapped
response format {"bars":{"BTC/USD":[...]}} vs stock {"bars":[...]}.
36 lines
916 B
Go
36 lines
916 B
Go
// Package db provides PostgreSQL connection management for the broker service.
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// NewPostgresPool creates a pgxpool connection pool from a database URL.
|
|
// The caller is responsible for closing the pool when done.
|
|
func NewPostgresPool(ctx context.Context, databaseURL string) (*pgxpool.Pool, error) {
|
|
config, err := pgxpool.ParseConfig(databaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse database URL: %w", err)
|
|
}
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create connection pool: %w", err)
|
|
}
|
|
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping database: %w", err)
|
|
}
|
|
|
|
return pool, nil
|
|
}
|
|
|
|
// HealthCheck pings the database to verify connectivity.
|
|
func HealthCheck(ctx context.Context, pool *pgxpool.Pool) error {
|
|
return pool.Ping(ctx)
|
|
}
|