From 2b0ef9ec323dfdb534bfdead364d8543269ee931 Mon Sep 17 00:00:00 2001 From: Hamza <72913359+Hamza12700@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:31:25 +0000 Subject: [PATCH 1/5] feat: small styling tweaks in the home page and partial html golamg template --- partial-html/short-link.html | 9 +++++---- static/index.html | 28 +++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/partial-html/short-link.html b/partial-html/short-link.html index 2c75ba7..93fbf38 100644 --- a/partial-html/short-link.html +++ b/partial-html/short-link.html @@ -1,5 +1,6 @@ +
Long URL: {{ .OriginalUrl }}
-shawty/s/{{ .ShortUrl }} -Long URL: {{ .OriginalUrl }}
- -Total clicks of your short URL + Total clicks of your short URL +Shawty is a free and open-source tool to shortening long URLs
-+ Shawty allows to shorten long links from Instagram, + Facebook, YouTube, + Twitter, Linked In, + WhatsApp, TikTok, + blogs and sites. Just paste the long URL and click the Shorten URL button. Copy the generated shortened URL and + share it on sites, chat and emails. After shortening the URL. +
+ ++ Your shortened URLs can be used in publications, documents, advertisements, blogs, forums, instant messages, and other + locations. Track statistics for your business and projects by monitoring the number of hits from your URL with our click + counter. +
From 669d6205f759532ccf51fc46c7c443cbc303617a Mon Sep 17 00:00:00 2001 From: Hamza <72913359+Hamza12700@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:42:24 +0000 Subject: [PATCH 2/5] feat: display when was the last time that short link was accessed --- handlers/stats.go | 12 +++++++++--- static/stat.html | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/handlers/stats.go b/handlers/stats.go index 5016f6b..4ea68e7 100644 --- a/handlers/stats.go +++ b/handlers/stats.go @@ -4,7 +4,9 @@ import ( "html/template" "log" "net/http" + "time" + "github.com/mergestat/timediff" "github.com/wavly/shawty/asserts" "github.com/wavly/shawty/database" ) @@ -12,6 +14,8 @@ import ( type AccessCount struct { ShortLink Count int + + LastAccessed string } func Stats(w http.ResponseWriter, r *http.Request) { @@ -30,7 +34,7 @@ func Stats(w http.ResponseWriter, r *http.Request) { db := database.ConnectDB() defer db.Close() - rows, err := db.Query("select accessed_count, original_url from urls where code = ?", inputCode) + rows, err := db.Query("select accessed_count, original_url, last_accessed from urls where code = ?", inputCode) if err != nil { http.Error(w, "Sorry, an unexpected error occur", http.StatusInternalServerError) log.Printf("Database error when selecting accessed_count and original_url where code = %s, Error %s\n", inputCode, err) @@ -40,6 +44,7 @@ func Stats(w http.ResponseWriter, r *http.Request) { var accessedCount int var originalUrl string + var lastAccessed time.Time // Redirect if no result is found if !rows.Next() { @@ -48,7 +53,7 @@ func Stats(w http.ResponseWriter, r *http.Request) { } // Scan the result - err = rows.Scan(&accessedCount, &originalUrl) + err = rows.Scan(&accessedCount, &originalUrl, &lastAccessed) if err != nil { http.Error(w, "Sorry, an unexpected error occur", http.StatusInternalServerError) log.Printf("Error scanning the result: %s", err) @@ -57,8 +62,9 @@ func Stats(w http.ResponseWriter, r *http.Request) { data := AccessCount{ Count: accessedCount, + LastAccessed: timediff.TimeDiff(lastAccessed), ShortLink: ShortLink{ - ShortUrl: inputCode, + ShortUrl: inputCode, OriginalUrl: originalUrl, }, } diff --git a/static/stat.html b/static/stat.html index 050b228..1b1a3e4 100644 --- a/static/stat.html +++ b/static/stat.html @@ -17,10 +17,11 @@The number of clicks from the shortened URL that redirected the user to the destination page.
+The number of clicks from the shortened URL that redirected the user to the destination page.
shawty/s/{{ .ShortUrl }} -Original URL : {{ .OriginalUrl }}
+Original URL : {{ .OriginalUrl }}