Add .gitignore for generated database files and implement database connection functionality

This commit is contained in:
Luis Eduardo Jeréz Girón
2024-07-20 00:29:09 -06:00
parent 76d825595f
commit ef12cece4f
2 changed files with 38 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
# Ignore the generated files to avoid git conflicts
dbgen/
+36
View File
@@ -0,0 +1,36 @@
package db
import (
"database/sql"
"github.com/eduardolat/pgbackweb/internal/config"
"github.com/eduardolat/pgbackweb/internal/logger"
_ "github.com/lib/pq"
)
func Connect(env *config.Env) *sql.DB {
db, err := sql.Open("postgres", *env.PBW_POSTGRES_CONN_STRING)
if err != nil {
logger.FatalError(
"Could not connect to DB",
logger.KV{
"error": err,
},
)
}
err = db.Ping()
if err != nil {
logger.FatalError(
"Could not ping DB",
logger.KV{
"error": err,
},
)
}
db.SetMaxOpenConns(10)
logger.Info("Connected to DB")
return db
}