1234567891011121314151617181920212223242526272829303132333435363738394041 |
- package infrastructure
- import (
- "database/sql"
- "github.com/spf13/viper"
- _ "github.com/go-sql-driver/mysql"
- )
- 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.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
- }
|