infratructure.go 1.1 KB

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