Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: merge #20

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/joho/godotenv v1.5.1
require (
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/coder/websocket v1.8.12 // indirect
github.com/mergestat/timediff v0.0.3 // indirect
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NA
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mergestat/timediff v0.0.3 h1:ucCNh4/ZrTPjFZ081PccNbhx9spymCJkFxSzgVuPU+Y=
github.com/mergestat/timediff v0.0.3/go.mod h1:yvMUaRu2oetc+9IbPLYBJviz6sA7xz8OXMDfhBl7YSI=
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d h1:dOMI4+zEbDI37KGb0TI44GUAwxHF9cMsIoDTJ7UmgfU=
github.com/tursodatabase/libsql-client-go v0.0.0-20240902231107-85af5b9d094d/go.mod h1:l8xTsYB90uaVdMHXMCxKKLSgw5wLYBwBKKefNIUnm9s=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
Expand Down
2 changes: 1 addition & 1 deletion handlers/shawty.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func Shawty(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := ShortLink{
ShortUrl: code,
ShortUrl: hashUrl,
OriginalUrl: longUrl,
}
asserts.NoErr(templ.Execute(w, data), "Failed to execute template short-link.html")
Expand Down
12 changes: 9 additions & 3 deletions handlers/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"html/template"
"log"
"net/http"
"time"

"github.com/mergestat/timediff"
"github.com/wavly/shawty/asserts"
"github.com/wavly/shawty/database"
)

type AccessCount struct {
ShortLink
Count int

LastAccessed string
}

func Stats(w http.ResponseWriter, r *http.Request) {
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -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)
Expand All @@ -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,
},
}
Expand Down
9 changes: 5 additions & 4 deletions partial-html/short-link.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="flex flex-col items-center gap-2 text-lg font-mono mb-2 mt-10 text-center">
<a class="underline text-white" href="/s/{{ .ShortUrl }}">shawty/s/{{ .ShortUrl }}</a>
<p>Long URL: <a class="underline" href="{{ .OriginalUrl }}">{{ .OriginalUrl }}</a></p>

<a class="underline text-white" href="/s/{{ .ShortUrl }}">shawty/s/{{ .ShortUrl }}</a>
<p>Long URL: <a class="underline" href="{{ .OriginalUrl }}">{{ .OriginalUrl }}</a></p>

<a class="my-3 hover:underline w-fit p-3 rounded-md bg-zinc-700" href="/stat/{{ .ShortUrl }}">Total clicks of your short URL</a>
<a class="my-3 hover:underline w-fit p-3 rounded-md bg-zinc-700" href="/stat/{{ .ShortUrl }}">Total clicks of your short URL</a>
</div>
2 changes: 1 addition & 1 deletion static/dist.css

Large diffs are not rendered by default.

28 changes: 25 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
crossorigin="anonymous"></script>
<title>Shawty</title>
<link rel="stylesheet" href="dist.css">
<style>
a {
text-decoration: underline;
}
</style>
</head>

<body class="bg-[#090909] text-[#ada9a9] mt-10">
<body class="bg-[#090909] w-[55rem] mx-auto text-[#ada9a9] mt-10">
<h1 class="text-5xl text-center text-white mb-2 font-bold">Shawty</h1>
<p class="text-center text-lg mb-10">Shawty is a free and <a class="underline"
href="https://github.com/wavly/shawty">open-source</a> tool to shortening long URLs</p>
<div class="py-3 px-7 relative rounded-md bg-[#1f1f1f] max-w-[45rem] mx-auto">
<div class="py-3 px-7 my-4 relative rounded-md bg-[#1f1f1f] mx-auto">
<h2 class="text-3xl font-bold text-center">Paste the URL to be shortened</h2>
<form class="flex justify-center my-6" hx-post="/shawty" hx-target="#return">
<input name="url" type="text" placeholder="Enter the Link here"
Expand All @@ -27,8 +32,25 @@ <h2 class="text-3xl font-bold text-center">Paste the URL to be shortened</h2>
</button>
<img class="htmx-indicator absolute right-[2em] w-10" src="https://i.gifer.com/XVo6.gif" alt="spinner">
</form>
<span id="return" class="flex flex-col items-center gap-2 text-lg font-mono my-10 text-center"></span>
<span id="return"></span>
</div>

<h2 class="font-bold mb-2 mt-10 text-3xl">Simple and fast URL shortener!</h2>
<p class="text-lg">
Shawty allows to shorten long links from <a href="https://www.instagram.com">Instagram</a>,
<a href="https://facebook.com">Facebook</a>, <a href="https://youtube.com">YouTube</a>,
<a href="https://x.com">Twitter</a>, <a href="https://linkedin.com">Linked In</a>,
<a href="https://whatsapp.com">WhatsApp</a>, <a href="https://tiktok.com">TikTok</a>,
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.
</p>

<h2 class="font-bold mb-2 mt-10 text-3xl">Shorten, share and track</h2>
<p class="text-lg mb-5">
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.
</p>
</body>

</html>
5 changes: 3 additions & 2 deletions static/stat.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
<h1 class="text-5xl text-center text-white font-bold">Shawty</h1>
<div class="w-[56rem] flex flex-col gap-3 my-8 mx-auto">
<h2 class="text-3xl font-bold font-mono">Total number of clicks</h2>
<p>The number of clicks from the shortened URL that redirected the user to the destination page.</p>
<p class="text-lg">The number of clicks from the shortened URL that redirected the user to the destination page.</p>
<a class="underline bg-[#292929] font-mono p-2 w-fit rounded-md text-white" href="/s/{{ .ShortUrl }}">shawty/s/{{ .ShortUrl }}</a>
<p class="text-lg font-bold flex gap-3">Original URL : <a class="underline font-mono" href="{{ .OriginalUrl }}">{{ .OriginalUrl }}</a></p>
<p class="text-lg font-bold flex gap-3">Original URL : <a class="underline font-mono" href="{{ .OriginalUrl }}">{{ .OriginalUrl }}</a></p>
<h3 class="font-bold text-xl font-mono">Count: {{ .Count }}</h3>
<h3 class="font-bold text-xl font-mono">Last Accessed: {{ .LastAccessed }}</h3>
</div>
</body>

Expand Down
Loading