Created
January 18, 2021 13:22
-
-
Save marioarizaj/b47e7010adbf9dad0424d8bed50b2818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type StorageIFace interface { | |
} | |
type Storage struct { | |
db *pg.DB | |
} | |
var sto StorageIFace | |
func ConnectToDB() (StorageIFace, error) { | |
if sto != nil { | |
return sto, nil | |
} | |
db := pg.Connect(&pg.Options{ | |
Addr: fmt.Sprintf("%s:%s", os.Getenv("DB_HOST"), os.Getenv("DB_PORT")), | |
User: os.Getenv("DB_USER"), | |
Password: os.Getenv("DB_PASSWORD"), | |
Database: os.Getenv("DB_NAME"), | |
}) | |
err := db.Ping(context.Background()) | |
if err != nil { | |
return nil, err | |
} | |
sto = &Storage{db: db} | |
return sto, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment