Skip to content

Commit

Permalink
refactor: extract db initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pcvolkmer committed Dec 2, 2024
1 parent 3950eb5 commit bf09b67
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,16 @@ func main() {
TLSConfig: cli.Ssl,
}

if dbx, err := sql.Open("mysql", dbCfg.FormatDSN()); err == nil {
if err := dbx.Ping(); err == nil {
db = dbx
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Println("Cannot close database connection")
}
}(db)
} else {
log.Fatalf("Cannot connect to Database: %s\n", err.Error())
}
} else {
log.Fatalf("Cannot connect to Database: %s\n", err.Error())
db, dbErr := initDb(dbCfg)
if dbErr != nil {
log.Fatalf("Cannot connect to Database: %s\n", dbErr.Error())
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
log.Println("Cannot close database connection")
}
}(db)

if cli.OcaPlus {
patients := InitPatients(db)
Expand All @@ -162,6 +157,18 @@ func main() {
}
}

func initDb(dbCfg mysql.Config) (*sql.DB, error) {
if dbx, err := sql.Open("mysql", dbCfg.FormatDSN()); err == nil {
if err := dbx.Ping(); err == nil {
return dbx, nil
} else {
return nil, err
}
} else {
return nil, err
}
}

func AnonymizedID(id string) string {
if cli.NoAnon {
return id
Expand Down

0 comments on commit bf09b67

Please sign in to comment.