block-accounting/backend/internal/infrastructure/repository/db.go

32 lines
601 B
Go
Raw Normal View History

2024-05-24 17:44:24 +00:00
package repository
import (
"database/sql"
"fmt"
"github.com/emochka2007/block-accounting/internal/pkg/config"
_ "github.com/lib/pq"
)
func ProvideDatabaseConnection(c config.Config) (*sql.DB, func(), error) {
sslmode := "disable"
if c.DB.EnableSSL {
sslmode = "enable"
}
connStr := fmt.Sprintf(
"postgresql://%s:%s@%s/%s?sslmode=%s",
c.DB.User, c.DB.Secret, c.DB.Host, c.DB.Database, sslmode,
)
db, err := sql.Open("postgres", connStr)
if err != nil {
return nil, func() {}, fmt.Errorf("error connecting to database: %w", err)
}
return db, func() {
db.Close()
}, nil
}