Skip to content

Commit

Permalink
Move the fetching of database connection details to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobefu committed Nov 28, 2024
1 parent 87915aa commit 3d50357
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions cmd/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,10 @@ import (
var DB *sql.DB

func Connect() error {
connString := os.Getenv("DB_CONN")

if connString == "" {
return errors.New("DB_CONN is not set")
}

dbType := os.Getenv("DB_TYPE")
connString, dbType, err := getConnectionDetails()

if dbType == "" {
return errors.New("DB_TYPR is not set")
if err != nil {
return err
}

db, err := sql.Open(dbType, connString)
Expand All @@ -33,3 +27,19 @@ func Connect() error {
DB = db
return nil
}

func getConnectionDetails() (string, string, error) {
connString := os.Getenv("DB_CONN")

if connString == "" {
return "", "", errors.New("DB_CONN is not set")
}

dbType := os.Getenv("DB_TYPE")

if dbType == "" {
return "", "", errors.New("DB_TYPE is not set")
}

return connString, dbType, nil
}

0 comments on commit 3d50357

Please sign in to comment.