Skip to content

Commit 340adb1

Browse files
gabrielwong159willnorris
authored andcommitted
golink: add .all page to display all existing links
Signed-off-by: Gabriel Wong <[email protected]>
1 parent 789e2a8 commit 340adb1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

golink.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func Run() error {
133133
http.HandleFunc("/.export", serveExport)
134134
http.HandleFunc("/.help", serveHelp)
135135
http.HandleFunc("/.opensearch", serveOpenSearch)
136+
http.HandleFunc("/.all", serveAll)
136137
http.Handle("/.static/", http.StripPrefix("/.", http.FileServer(http.FS(embeddedFS))))
137138

138139
if *dev != "" {
@@ -193,6 +194,9 @@ var (
193194
// helpTmpl is the template used by the http://go/.help page
194195
helpTmpl *template.Template
195196

197+
// allTmpl is the template used by the http://go/.all page
198+
allTmpl *template.Template
199+
196200
// opensearchTmpl is the template used by the http://go/.opensearch page
197201
opensearchTmpl *template.Template
198202
)
@@ -213,6 +217,7 @@ func init() {
213217
detailTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/detail.html"))
214218
successTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/success.html"))
215219
helpTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/help.html"))
220+
allTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/all.html"))
216221
opensearchTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/opensearch.xml"))
217222
}
218223

@@ -282,6 +287,24 @@ func serveHome(w http.ResponseWriter, short string) {
282287
})
283288
}
284289

290+
func serveAll(w http.ResponseWriter, _ *http.Request) {
291+
if err := flushStats(); err != nil {
292+
http.Error(w, err.Error(), http.StatusInternalServerError)
293+
return
294+
}
295+
296+
links, err := db.LoadAll()
297+
if err != nil {
298+
http.Error(w, err.Error(), http.StatusInternalServerError)
299+
return
300+
}
301+
sort.Slice(links, func(i, j int) bool {
302+
return links[i].Short < links[j].Short
303+
})
304+
305+
allTmpl.Execute(w, links)
306+
}
307+
285308
func serveHelp(w http.ResponseWriter, _ *http.Request) {
286309
helpTmpl.Execute(w, nil)
287310
}

tmpl/all.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{ define "main" }}
2+
<h2 class="text-xl font-bold pt-6 pb-2">All Links</h2>
3+
<table class="table-auto ">
4+
<thead class="border-b border-gray-200 uppercase text-xs text-gray-500 text-left">
5+
<tr>
6+
<th class="p-2">Link</th>
7+
<th class="p-2">Long</th>
8+
<th class="p-2">Owner</th>
9+
<th class="p-2">Created</th>
10+
<th class="p-2">Last Edit</th>
11+
</tr>
12+
</thead>
13+
<tbody>
14+
{{ range . }}
15+
<tr class="hover:bg-gray-100 group border-b border-gray-200">
16+
<td class="flex">
17+
<a class="block flex-1 p-2 pr-4 hover:text-blue-500 hover:underline" href="/{{ .Short }}">go/{{ .Short }}</a>
18+
<a class="flex items-center px-2 invisible group-hover:visible" title="Link Details" href="/.detail/{{ .Short }}">
19+
<svg class="hover:fill-blue-500" xmlns="http://www.w3.org/2000/svg" height="1.3em" viewBox="0 0 24 24" width="1.3em" fill="#000000" stroke-width="2"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 7h2v2h-2zm0 4h2v6h-2zm1-9C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"/></svg>
20+
</a>
21+
</td>
22+
<td class="p-2">{{ .Long }}</td>
23+
<td class="p-2">{{ .Owner }}</td>
24+
<td class="p-2">{{ .Created }}</td>
25+
<td class="p-2">{{ .LastEdit }}</td>
26+
</tr>
27+
{{ end }}
28+
</tbody>
29+
</table>
30+
{{ end }}

0 commit comments

Comments
 (0)