mirror of
https://github.com/emo2007/block-accounting.git
synced 2024-11-10 04:36:26 +00:00
32 lines
601 B
Go
32 lines
601 B
Go
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
|
|
}
|