Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Load connection URI from ENV config
func connectionURIFromEnvConfig() string {
errMsg := "no valid connection string provided"
connConfig, err := url.Parse(os.Getenv("DB_PORT"))
if err != nil {
log.Fatalln(errMsg)
}
dbHost, dbPort, err := net.SplitHostPort(connConfig.Host)
if err != nil {
log.Fatalln(errMsg)
}
dbName := os.Getenv("DB_DATABASE")
dbUser := os.Getenv("DB_USER")
dbPass := os.Getenv("DB_PASSWORD")
if dbHost == "" || dbPort == "" || dbName == "" || dbUser == "" || dbPass == "" {
log.Fatalln(errMsg)
}
return "postgres://" + dbUser + ":" + dbPass + "@" + dbHost + "/" + dbName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.