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)
|
||||
|
||||
);
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -12,8 +13,11 @@ import (
|
||||
"github.com/ferdzo/ferurl/internal/db"
|
||||
"github.com/ferdzo/ferurl/utils"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logrus.New()
|
||||
|
||||
type Service struct {
|
||||
cache *cache.Cache
|
||||
database *db.Database
|
||||
@@ -25,12 +29,17 @@ type Handler struct {
|
||||
}
|
||||
|
||||
func NewService(redisConfig utils.RedisConfig, databaseConfig utils.DatabaseConfig) (*Service, error) {
|
||||
log.Info("Initializing Redis client")
|
||||
redisClient, err := cache.NewRedisClient(redisConfig)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to initialize Redis client")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info("Initializing Database client")
|
||||
databaseClient, err := db.NewDatabaseClient(databaseConfig)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to initialize Database client")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -63,28 +72,34 @@ func (h *Handler) CreateShortURL(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
if err := decoder.Decode(&input); err != nil {
|
||||
log.WithError(err).Warn("Invalid JSON input received")
|
||||
http.Error(w, "Invalid JSON input", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
longUrl := input.URL
|
||||
if longUrl == "" {
|
||||
log.Warn("Empty URL received")
|
||||
http.Error(w, "Long URL is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !utils.IsValidUrl(longUrl) {
|
||||
log.WithField("url", longUrl).Warn("Invalid URL format received")
|
||||
http.Error(w, "Invalid URL", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
expiresAt := input.ExpiresAt
|
||||
|
||||
shortUrl := generateShortUrl(longUrl)
|
||||
fetchedShortUrl, _ := h.fetchUrl(shortUrl)
|
||||
if fetchedShortUrl != "" {
|
||||
fetchedShortUrl, err := h.fetchUrl(shortUrl)
|
||||
if err == nil && fetchedShortUrl != "" {
|
||||
fullShortUrl := fmt.Sprintf("%s%s", h.baseURL, shortUrl)
|
||||
json.NewEncoder(w).Encode(map[string]string{"short_url": fullShortUrl})
|
||||
return
|
||||
}
|
||||
if err != nil && isNotFoundError(err) {
|
||||
log.WithField("short_url", shortUrl).Debug("Creating new short URL")
|
||||
}
|
||||
|
||||
newUrl := db.URL{
|
||||
URL: longUrl,
|
||||
@@ -93,7 +108,7 @@ func (h *Handler) CreateShortURL(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
if err := h.service.storeUrl(newUrl); err != nil {
|
||||
fmt.Println(err)
|
||||
log.Error("Failed to store URL", "error", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -113,19 +128,28 @@ func (h *Handler) GetUrl(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
shortKey := chi.URLParam(r, "key")
|
||||
if shortKey == "" {
|
||||
log.Warn("Empty short URL key received")
|
||||
http.Error(w, "ID is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if !utils.IsValidShortUrl(shortKey) {
|
||||
log.WithField("key", shortKey).Warn("Invalid short URL format received")
|
||||
http.Error(w, "Invalid short URL", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
longUrl, err := h.fetchUrl(shortKey)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Error(w, "URL not found", http.StatusInternalServerError)
|
||||
if isNotFoundError(err) {
|
||||
http.Error(w, "URL not found", http.StatusNotFound)
|
||||
} else {
|
||||
log.WithFields(logrus.Fields{
|
||||
"short_url": shortKey,
|
||||
"error": err.Error(),
|
||||
}).Error("Failed to fetch URL")
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -137,12 +161,16 @@ func (h *Handler) GetUrl(w http.ResponseWriter, r *http.Request) {
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err = h.service.database.InsertAnalytics(pv)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
go func(pageVisit db.PageVisit) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.WithField("error", r).Error("Panic in analytics insertion")
|
||||
}
|
||||
}()
|
||||
if err := h.service.database.InsertAnalytics(pageVisit); err != nil {
|
||||
logrus.Error("Failed to store analytics", "error", err)
|
||||
}
|
||||
}(pv)
|
||||
|
||||
http.Redirect(w, r, longUrl, http.StatusSeeOther)
|
||||
}
|
||||
@@ -161,9 +189,13 @@ func (h *Handler) fetchUrl(id string) (string, error) {
|
||||
}
|
||||
resultChan := make(chan result, 2)
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
url, err := h.service.fetchUrlFromCache(id)
|
||||
select {
|
||||
case resultChan <- result{url, err, "cache"}:
|
||||
@@ -173,6 +205,7 @@ func (h *Handler) fetchUrl(id string) (string, error) {
|
||||
}()
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
url, err := h.service.fetchUrlFromDatabase(id)
|
||||
select {
|
||||
case resultChan <- result{url, err, "db"}:
|
||||
@@ -181,65 +214,118 @@ func (h *Handler) fetchUrl(id string) (string, error) {
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(resultChan)
|
||||
}()
|
||||
|
||||
var foundInDB string
|
||||
var cacheHit bool
|
||||
var lastErr error
|
||||
|
||||
for range 2 {
|
||||
res := <-resultChan
|
||||
if res.err == nil {
|
||||
if res.source == "db" {
|
||||
foundInDB = res.url
|
||||
if !cacheHit {
|
||||
continue
|
||||
}
|
||||
} else if res.source == "cache" {
|
||||
cacheHit = true
|
||||
if foundInDB != "" {
|
||||
return res.url, nil
|
||||
}
|
||||
select {
|
||||
case res, ok := <-resultChan:
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
return res.url, nil
|
||||
if res.err == nil {
|
||||
if res.source == "db" {
|
||||
foundInDB = res.url
|
||||
if !cacheHit {
|
||||
continue
|
||||
}
|
||||
} else if res.source == "cache" {
|
||||
cacheHit = true
|
||||
if foundInDB != "" {
|
||||
return res.url, nil
|
||||
}
|
||||
}
|
||||
return res.url, nil
|
||||
}
|
||||
lastErr = res.err
|
||||
case <-ctx.Done():
|
||||
return "", fmt.Errorf("timeout while fetching URL: %w", ctx.Err())
|
||||
}
|
||||
lastErr = res.err
|
||||
}
|
||||
|
||||
if foundInDB != "" {
|
||||
go func() {
|
||||
_ = h.service.cache.Set(id, foundInDB)
|
||||
fmt.Println("URL updated in Redis")
|
||||
}()
|
||||
go func(cacheKey, url string) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Error("Panic in cache update", "error", r)
|
||||
}
|
||||
}()
|
||||
if err := h.service.cache.Set(cacheKey, url); err != nil {
|
||||
log.Error("Failed to update URL in cache", "error", err)
|
||||
}
|
||||
}(id, foundInDB)
|
||||
|
||||
return foundInDB, nil
|
||||
}
|
||||
|
||||
if isNotFoundError(lastErr) {
|
||||
log.Warn("URL not found")
|
||||
return "", nil
|
||||
}
|
||||
return "", lastErr
|
||||
}
|
||||
|
||||
func (s *Service) fetchUrlFromCache(shortUrl string) (string, error) {
|
||||
if url, err := s.cache.Get(shortUrl); err == nil {
|
||||
return url, nil
|
||||
url, err := s.cache.Get(shortUrl)
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"short_url": shortUrl,
|
||||
"error": err,
|
||||
}).Debug("Cache miss")
|
||||
return "", err
|
||||
}
|
||||
return "", fmt.Errorf("URL not found")
|
||||
return url, nil
|
||||
|
||||
}
|
||||
|
||||
func (s *Service) fetchUrlFromDatabase(shortUrl string) (string, error) {
|
||||
if url, err := s.database.GetURL(shortUrl); err == nil {
|
||||
return url, nil
|
||||
url, err := s.database.GetURL(shortUrl)
|
||||
if err != nil {
|
||||
log.WithFields(logrus.Fields{
|
||||
"short_url": shortUrl,
|
||||
"error": err,
|
||||
}).Debug("Database lookup failed")
|
||||
return "", err
|
||||
}
|
||||
log.WithField("short_url", shortUrl).Debug("Database hit")
|
||||
return url, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("URL not found")
|
||||
func isNotFoundError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
errMsg := err.Error()
|
||||
return strings.Contains(errMsg, "not found") ||
|
||||
strings.Contains(errMsg, "redis: nil") ||
|
||||
strings.Contains(errMsg, "no rows in result set")
|
||||
}
|
||||
|
||||
func (s *Service) storeUrl(u db.URL) error {
|
||||
var wg sync.WaitGroup
|
||||
errChan := make(chan error, 2)
|
||||
|
||||
wg.Add(1)
|
||||
wg.Add(2)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := s.cache.Set(u.ShortURL, u.URL); err != nil {
|
||||
errChan <- err
|
||||
log.WithFields(logrus.Fields{
|
||||
"short_url": u.ShortURL,
|
||||
"error": err,
|
||||
}).Error("Failed to store URL in cache")
|
||||
select {
|
||||
case errChan <- err:
|
||||
default:
|
||||
}
|
||||
} else {
|
||||
log.WithField("short_url", u.ShortURL).Debug("Stored URL in cache")
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -247,18 +333,26 @@ func (s *Service) storeUrl(u db.URL) error {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := s.database.InsertNewURL(u); err != nil {
|
||||
errChan <- err
|
||||
log.WithFields(logrus.Fields{
|
||||
"short_url": u.ShortURL,
|
||||
"error": err,
|
||||
}).Error("Failed to store URL in database")
|
||||
select {
|
||||
case errChan <- err:
|
||||
default:
|
||||
}
|
||||
} else {
|
||||
log.WithField("short_url", u.ShortURL).Debug("Stored URL in database")
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(errChan)
|
||||
}()
|
||||
wg.Wait()
|
||||
close(errChan)
|
||||
|
||||
for err := range errChan {
|
||||
select {
|
||||
case err := <-errChan:
|
||||
return err
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user