From 92295654e7ab76ba15668da4e90873602732dc0d Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 12 Mar 2026 11:29:14 +0100 Subject: [PATCH] feat: defer function, add info to string var dsn --- projects/greenlight/cmd/api/main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/projects/greenlight/cmd/api/main.go b/projects/greenlight/cmd/api/main.go index 98423e4..088da3d 100644 --- a/projects/greenlight/cmd/api/main.go +++ b/projects/greenlight/cmd/api/main.go @@ -54,7 +54,7 @@ func main() { flag.StringVar(&cfg.env, "env", "development", "Environment (development|staging|production)") // Read the DSN value from the db-dsn command-line flag into the config struct. We // default to using our development DSN if no flag is provided. - flag.StringVar(&cfg.db.dsn, "db-dsn", dsn, "PostgreSQL DSN") + flag.StringVar(&cfg.db.dsn, "db-dsn", dsn, "PostgreSQL DSN (From DATABASE_DSN in .env)") flag.Parse() @@ -68,8 +68,11 @@ func main() { // Defer a call to db.Close() so that the connection pool is closed before the // main() function exits. - defer db.Close() - + defer func() { + if closeErr := db.Close(); closeErr != nil && err == nil { + err = closeErr + } + }() // Also log a message to say that the connection pool has been successfully // established. logger.Printf("database connection pool established")