infratructure.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package infrastructure
  2. import (
  3. "database/sql"
  4. "github.com/spf13/viper"
  5. )
  6. const exampleValidHTTPURL = "https://example.com"
  7. /**
  8. DbDial is almost a proxy to sql.Open but guarantees that db will be nil if err is not nil.
  9. */
  10. func DbDial(dbDriver, dbDsn string) (*sql.DB, error) {
  11. db, err := sql.Open(dbDriver, dbDsn)
  12. if err != nil {
  13. return nil, err
  14. }
  15. return db, nil
  16. }
  17. /**
  18. ParseDbCred returns DB information from the supported configuration sources.
  19. */
  20. func ParseDbCred() (driver, dsn string) {
  21. viper.SetDefault("database.driver", "mysql")
  22. viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
  23. driver = viper.Get("database.driver").(string)
  24. dsn = viper.Get("database.dsn").(string)
  25. dsn += "?parseTime=true"
  26. return
  27. }
  28. /**
  29. ParseDbCred returns Test DB information from the supported configuration sources.
  30. */
  31. func ParseTestDbCred() (driver, dsn string) {
  32. viper.SetDefault("database.driver", "mysql")
  33. viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
  34. viper.SetDefault("database.test_dsn", "root:root@tcp(localhost:3306)/kurz_test")
  35. driver = viper.Get("database.driver").(string)
  36. dsn = viper.Get("database.test_dsn").(string)
  37. dsn += "?parseTime=true"
  38. return
  39. }