Files
2026-05-12 18:49:52 +08:00

23 lines
601 B
Go

package repository
import (
"context"
"fmt"
"os"
)
// NewRepository creates a Repository based on environment variables.
// If DATABASE_URL is set, connects to PostgreSQL via pgx.
// Otherwise returns a new MemoryRepository.
func NewRepository(ctx context.Context) (Repository, func(), error) {
if connString := os.Getenv("DATABASE_URL"); connString != "" {
repo, err := NewPostgresRepository(ctx, connString)
if err != nil {
return nil, nil, fmt.Errorf("postgres: %w", err)
}
return repo, func() { repo.Close() }, nil
}
repo := NewMemoryRepository()
return repo, func() {}, nil
}