Fixed database migration, imporved logging, imporved hashing function,
introduced connection pooling instead of single conncetion.
This commit is contained in:
@@ -7,11 +7,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ferdzo/ferurl/utils"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
client *pgx.Conn
|
||||
connectionPool *pgxpool.Pool
|
||||
}
|
||||
|
||||
type URL struct {
|
||||
@@ -29,19 +29,19 @@ type PageVisit struct {
|
||||
}
|
||||
|
||||
func NewDatabaseClient(config utils.DatabaseConfig) (*Database, error) {
|
||||
conn, err := pgx.Connect(context.Background(), utils.DatabaseUrl())
|
||||
pool, err := pgxpool.New(context.Background(), utils.DatabaseUrl())
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Database{client: conn}, nil
|
||||
return &Database{connectionPool: pool}, nil
|
||||
}
|
||||
|
||||
func (d *Database) InsertNewURL(u URL) error {
|
||||
fmt.Print("InsertNewURL called with URL: ", u.URL)
|
||||
timeNow := time.Now()
|
||||
_, err := d.client.Exec(context.Background(), "INSERT INTO urls (shorturl, url, created_at,expires_at,active) VALUES ($1, $2, $3,$4,$5)", u.ShortURL, u.URL, timeNow, u.ExpiresAt, true)
|
||||
_, err := d.connectionPool.Exec(context.Background(), "INSERT INTO urls (shorturl, url, created_at,expires_at,active) VALUES ($1, $2, $3,$4,$5)", u.ShortURL, u.URL, timeNow, u.ExpiresAt, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to insert URL into database: %w", err)
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (d *Database) InsertNewURL(u URL) error {
|
||||
|
||||
func (d *Database) InsertAnalytics(p PageVisit) error {
|
||||
timeNow := time.Now()
|
||||
_, err := d.client.Exec(context.Background(), "INSERT INTO analytics (shorturl, count, ip_address, user_agent, created_at) VALUES ($1, $2, $3, $4, $5)", p.ShortURL, p.Count, p.IP_Address, p.UserAgent, timeNow)
|
||||
_, err := d.connectionPool.Exec(context.Background(), "INSERT INTO analytics (shorturl, count, ip_address, user_agent, created_at) VALUES ($1, $2, $3, $4, $5)", p.ShortURL, p.Count, p.IP_Address, p.UserAgent, timeNow)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to insert analytics into database: %w", err)
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func (d *Database) InsertAnalytics(p PageVisit) error {
|
||||
}
|
||||
|
||||
func (d *Database) GetAnalytics(shorturl string) ([]PageVisit, error) {
|
||||
rows, err := d.client.Query(context.Background(), "SELECT shorturl, count, ip_address, user_agent, created_at FROM analytics WHERE shorturl = $1", shorturl)
|
||||
rows, err := d.connectionPool.Query(context.Background(), "SELECT shorturl, count, ip_address, user_agent, created_at FROM analytics WHERE shorturl = $1", shorturl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to retrieve analytics from database: %w", err)
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (d *Database) GetAnalytics(shorturl string) ([]PageVisit, error) {
|
||||
}
|
||||
|
||||
func (d *Database) DeleteURL(shorturl string) error {
|
||||
_, err := d.client.Exec(context.Background(), "DELETE FROM urls WHERE shorturl = $1", shorturl)
|
||||
_, err := d.connectionPool.Exec(context.Background(), "DELETE FROM urls WHERE shorturl = $1", shorturl)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to delete URL from database: %w", err)
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func (d *Database) DeleteURL(shorturl string) error {
|
||||
|
||||
func (d *Database) GetURL(shorturl string) (string, error) {
|
||||
var url string
|
||||
err := d.client.QueryRow(context.Background(), "SELECT url FROM urls WHERE shorturl = $1", shorturl).Scan(&url)
|
||||
err := d.connectionPool.QueryRow(context.Background(), "SELECT url FROM urls WHERE shorturl = $1", shorturl).Scan(&url)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to retrieve URL from database: %w", err)
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
CREATE TABLE urls (
|
||||
shorturl VARCHAR(7) PRIMARY KEY,
|
||||
url VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP DEFAULT NULL,
|
||||
active BOOLEAN DEFAULT TRUE
|
||||
shorturl VARCHAR(7) PRIMARY KEY,
|
||||
url VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP DEFAULT NULL,
|
||||
active BOOLEAN DEFAULT TRUE
|
||||
|
||||
);
|
||||
|
||||
CREATE TABLE analytics (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
shorturl VARCHAR(7) NOT NULL,
|
||||
count INTEGER DEFAULT 0,
|
||||
ip_address VARCHAR(50) DEFAULT NULL,
|
||||
user_agent VARCHAR(1024) DEFAULT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
id SERIAL PRIMARY KEY,
|
||||
shorturl VARCHAR(7) NOT NULL,
|
||||
count INTEGER DEFAULT 0,
|
||||
ip_address VARCHAR(50) DEFAULT NULL,
|
||||
user_agent VARCHAR(1024) DEFAULT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
FOREIGN KEY (shorturl) REFERENCES urls(shorturl),
|
||||
FOREIGN KEY (shorturl) REFERENCES urls(shorturl)
|
||||
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user