(Feat): Initial Commit
This commit is contained in:
40
internal/persistence/postgres.go
Normal file
40
internal/persistence/postgres.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package persistence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"allaboutapps.dev/aw/go-starter/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
dbPingTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
func NewDB(cfg config.Database) (*sql.DB, error) {
|
||||
db, err := sql.Open("postgres", cfg.ConnectionString())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open db connection: %w", err)
|
||||
}
|
||||
|
||||
if cfg.MaxOpenConns > 0 {
|
||||
db.SetMaxOpenConns(cfg.MaxOpenConns)
|
||||
}
|
||||
if cfg.MaxIdleConns > 0 {
|
||||
db.SetMaxIdleConns(cfg.MaxIdleConns)
|
||||
}
|
||||
if cfg.ConnMaxLifetime > 0 {
|
||||
db.SetConnMaxLifetime(cfg.ConnMaxLifetime)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), dbPingTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to ping DB: %w", err)
|
||||
}
|
||||
|
||||
return db, nil
|
||||
}
|
||||
Reference in New Issue
Block a user