This commit is contained in:
@@ -29,15 +29,22 @@ func run() error {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
for _, key := range []string{"PGHOST", "PGDATABASE", "PGUSER", "PGPASSWORD"} {
|
||||
if os.Getenv(key) == "" {
|
||||
return fmt.Errorf("%s is required", key)
|
||||
databaseURL := os.Getenv("DATABASE_URL")
|
||||
if databaseURL == "" {
|
||||
for _, key := range []string{"PGHOST", "PGDATABASE", "PGUSER", "PGPASSWORD"} {
|
||||
if os.Getenv(key) == "" {
|
||||
return fmt.Errorf("%s is required when DATABASE_URL is unset", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use libpq environment variables so passwords need no URL escaping.
|
||||
pool, err := pgxpool.New(ctx, "")
|
||||
// Production uses a URL; local development retains libpq environment variables.
|
||||
pool, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
if databaseURL != "" {
|
||||
// Parser errors can include the URL, so do not log the credential-bearing input.
|
||||
return errors.New("configure database: invalid DATABASE_URL")
|
||||
}
|
||||
return fmt.Errorf("configure database: %w", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
Reference in New Issue
Block a user