diff --git a/main.go b/main.go index 9d621a3..8a5ea98 100644 --- a/main.go +++ b/main.go @@ -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) @@ -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