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

Add long URLs to Most Visited table on homepage #127

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ var (

type visitData struct {
Short string
Long string
NumClicks int
}

Expand Down Expand Up @@ -404,10 +405,21 @@ func serveHandler() http.Handler {
func serveHome(w http.ResponseWriter, r *http.Request, short string) {
var clicks []visitData

linkMap := map[string]string{}
links, err := db.LoadAll()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, link := range links {
linkMap[link.Short] = link.Long
}

stats.mu.Lock()
for short, numClicks := range stats.clicks {
clicks = append(clicks, visitData{
Short: short,
Long: linkMap[short],
NumClicks: numClicks,
})
}
Expand Down
2 changes: 2 additions & 0 deletions tmpl/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ <h2 class="text-xl font-bold pt-6 pb-2">Popular Links</h2>
<tr>
<th class="p-2">Link</th>
<th class="p-2">Clicks</th>
<th class="p-2">URL</th>
</tr>
</thead>
<tbody>
Expand All @@ -35,6 +36,7 @@ <h2 class="text-xl font-bold pt-6 pb-2">Popular Links</h2>
</a>
</td>
<td class="p-2">{{.NumClicks}}</td>
<td class="p-2"><a href="{{.Long}}">{{.Long}}</a></td>
</tr>
{{end}}
</tbody>
Expand Down