Skip to content

Commit

Permalink
add Link column to home.html
Browse files Browse the repository at this point in the history
Signed-off-by: shaftoes <[email protected]>
  • Loading branch information
shaftoes committed May 10, 2024
1 parent 41e9dc9 commit 1c704ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
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">{{.Long}}</td>
</tr>
{{end}}
</tbody>
Expand Down

0 comments on commit 1c704ff

Please sign in to comment.