12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package infrastructure
- import (
- "database/sql"
- "github.com/spf13/viper"
- )
- const exampleValidHTTPURL = "https://example.com"
- /**
- DbDial is almost a proxy to sql.Open but guarantees that db will be nil if err is not nil.
- */
- func DbDial(dbDriver, dbDsn string) (*sql.DB, error) {
- db, err := sql.Open(dbDriver, dbDsn)
- if err != nil {
- return nil, err
- }
- return db, nil
- }
- /**
- ParseDbCred returns DB information from the supported configuration sources.
- */
- func ParseDbCred() (driver, dsn string) {
- viper.SetDefault("database.driver", "mysql")
- viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
- driver = viper.Get("database.driver").(string)
- dsn = viper.Get("database.dsn").(string)
- dsn += "?parseTime=true"
- return
- }
- /**
- ParseDbCred returns Test DB information from the supported configuration sources.
- */
- func ParseTestDbCred() (driver, dsn string) {
- viper.SetDefault("database.driver", "mysql")
- viper.SetDefault("database.dsn", "root:root@tcp(localhost:3306)/kurz")
- viper.SetDefault("database.test_dsn", "root:root@tcp(localhost:3306)/kurz_test")
- driver = viper.Get("database.driver").(string)
- dsn = viper.Get("database.test_dsn").(string)
- dsn += "?parseTime=true"
- return
- }
|