Skip to content

Instantly share code, notes, and snippets.

@marioarizaj
Created January 18, 2021 13:22
Show Gist options
  • Save marioarizaj/b47e7010adbf9dad0424d8bed50b2818 to your computer and use it in GitHub Desktop.
Save marioarizaj/b47e7010adbf9dad0424d8bed50b2818 to your computer and use it in GitHub Desktop.
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