add env files
This commit is contained in:
16
database.go
16
database.go
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
// InitDB initializes the database connection
|
||||
// InitDB initializes the database connection (legacy function)
|
||||
func InitDB() error {
|
||||
// Get database URL from environment variable
|
||||
dbURL := os.Getenv("DATABASE_URL")
|
||||
@@ -19,6 +19,16 @@ func InitDB() error {
|
||||
// Default for local development
|
||||
dbURL = "postgres://postgres:password@localhost:5432/counter_db?sslmode=disable"
|
||||
}
|
||||
return initDBWithURL(dbURL)
|
||||
}
|
||||
|
||||
// InitDBWithConfig initializes the database connection with configuration
|
||||
func InitDBWithConfig(config *Config) error {
|
||||
return initDBWithURL(config.DatabaseURL)
|
||||
}
|
||||
|
||||
// initDBWithURL initializes the database connection with a specific URL
|
||||
func initDBWithURL(dbURL string) error {
|
||||
|
||||
var err error
|
||||
db, err = sql.Open("postgres", dbURL)
|
||||
@@ -31,7 +41,7 @@ func InitDB() error {
|
||||
return fmt.Errorf("failed to ping database: %w", err)
|
||||
}
|
||||
|
||||
log.Println("Database connection established")
|
||||
log.Println("✅ Database connection established successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -72,7 +82,7 @@ func CreateTables() error {
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Database tables created successfully")
|
||||
log.Println("✅ Database tables created successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user