-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e851c6d
commit b1b6886
Showing
7 changed files
with
290 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package driver | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
_ "github.com/jackc/pgconn" | ||
_ "github.com/jackc/pgx/v5" | ||
_ "github.com/jackc/pgx/v5/stdlib" | ||
|
||
|
||
) | ||
|
||
// DB holds the database connection pool | ||
type DB struct { | ||
SQL *sql.DB | ||
} | ||
|
||
var dbConn = &DB{} | ||
|
||
const maxOpenDbConn = 10 | ||
const MaxIdleDbConn = 5 | ||
const MaxDbLifetime = 5 * time.Minute | ||
|
||
// creates database pool for postgres | ||
func ConnectSQL(dsn string) (*DB, error) { | ||
d, err := NewDatabase(dsn) | ||
if err != nil { | ||
panic(err) | ||
} | ||
d.SetMaxOpenConns(maxOpenDbConn) | ||
d.SetMaxIdleConns(MaxIdleDbConn) | ||
d.SetConnMaxLifetime(MaxDbLifetime) | ||
dbConn.SQL = d | ||
err = testDB(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return dbConn, nil | ||
|
||
} | ||
|
||
// ping db | ||
func testDB(d *sql.DB) error { | ||
err := d.Ping() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// open new db | ||
func NewDatabase(dsn string) (*sql.DB, error) { | ||
db, err := sql.Open("pgx", dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err = db.Ping(); err != nil { | ||
return nil, err | ||
} | ||
return db, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dbrepo | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/CyberRoute/bruter/pkg/config" | ||
"github.com/CyberRoute/bruter/pkg/repository" | ||
) | ||
|
||
|
||
type postgresDBRepo struct { | ||
App *config.AppConfig | ||
DB *sql.DB | ||
} | ||
|
||
func NewPostgresRepo(conn *sql.DB, a *config.AppConfig) repository.DatabaseRepo { | ||
return &postgresDBRepo{ | ||
App: a, | ||
DB: conn, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dbrepo | ||
|
||
func (m *postgresDBRepo) AllUrls() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package repository | ||
|
||
type DatabaseRepo interface { | ||
AllUrls() bool | ||
} |