github.com/bibaroc/wingman@v0.0.2-0.20200911182922-33c2085136b1/pkg/db.go (about) 1 package pkg 2 3 import ( 4 "os" 5 "strconv" 6 ) 7 8 type DBConfiguration struct { 9 Host string 10 Port int 11 User string 12 Password string 13 DbName string 14 } 15 16 func dbConfiguration(prefix string) DBConfiguration { 17 s_port := os.Getenv(prefix + "DB__PORT") 18 port, err := strconv.Atoi(s_port) 19 if err != nil { 20 port = 5432 21 } 22 return DBConfiguration{ 23 Host: os.Getenv(prefix + "DB__HOST"), 24 Port: port, 25 User: os.Getenv(prefix + "DB__USER"), 26 Password: os.Getenv(prefix + "DB__PASSWORD"), 27 DbName: os.Getenv(prefix + "DB__DBNAME"), 28 } 29 } 30 31 func DBConfigurationFromEnv() DBConfiguration { return dbConfiguration("") } 32 func DBConfigurationFromEnvWithPrefix(prefix string) DBConfiguration { return dbConfiguration(prefix) }