//go:build !dev package dav import ( "context" "fmt" "os" "github.com/jackc/pgx/v5/pgxpool" ) var Service *BaseService // defaultPostgresHost is the fallback Postgres address used when // POSTGRES_HOST is unset — the pipeline/CI postgres service default. const defaultPostgresHost = "127.0.0.1" func init() { // Test builds wire their own pool in TestMain (testutils/testdb) — a real // connect here would race that and panic a bare-shell `go test`. Skip. if os.Getenv("GO_TESTING") != "" || os.Getenv("DAV_SKIP_INIT") != "" { return } if err := connect(); err != nil { panic("failed to initialize prod service: " + err.Error()) } } func connect() error { host := getEnv("POSTGRES_HOST") if host == "" { host = defaultPostgresHost } dsn := fmt.Sprintf( "postgres://%s:%s@%s:5432/%s?timezone=UTC&require_auth=scram-sha-256", getEnv("POSTGRES_USER"), getEnv("POSTGRES_PASSWORD"), host, getEnv("POSTGRES_DB"), ) pool, err := pgxpool.New(context.Background(), dsn) if err != nil { return err } Service = newBaseService(pool) return testDB(pool) } func testDB(pool *pgxpool.Pool) error { var n int err := pool.QueryRow(context.Background(), "SELECT 1").Scan(&n) if err != nil || n != 1 { return fmt.Errorf("db test failed: %w", err) } return nil } func getEnv(key string) string { if v := os.Getenv(key); v != "" { return v } // Return empty string instead of fatal error - allows tests to run without prod env vars return "" }