cmd/golink: move home template into separate file

This sets up an embed.FS even though we only have the one file right
now, as we will have more shortly.

Change-Id: I1292e4c00602fbfc3a7bc2a5f93a8c9b9e6715c0
This commit is contained in:
Will Norris
2022-06-07 13:49:29 -07:00
parent 7f2e1430f8
commit 259ce77cdc
2 changed files with 22 additions and 18 deletions
+5 -18
View File
@@ -4,6 +4,7 @@ package main
import (
"bufio"
"bytes"
"embed"
_ "embed"
"encoding/json"
"flag"
@@ -41,6 +42,9 @@ var stats struct {
//go:embed link-snapshot.json
var lastSnapshot []byte
//go:embed *.html
var embeddedFS embed.FS
var localClient *tailscale.LocalClient
// DiskLink is the JSON structure stored on disk in a file for each go short link.
@@ -126,24 +130,7 @@ type homeData struct {
}
func init() {
homeCreate = template.Must(template.New("home").Parse(`<html>
<body>
<h1>go/</h1>
shortlink service.
<h2>create</h2>
<form method="POST" action="/">
http://go/<input name=short size=20 value="{{.Short}}"> ==&gt; <input name=long size=40> <input type=submit value="create">
</form>
<h2>recent popular links</h2>
<table>
<tr><th>short link</th><th>num clicks</th></tr>
{{range .Clicks}}
<tr><td><a href="http://go/{{.Short}}">http://go/{{.Short}}</a></td><td>{{.NumClicks}}</td></tr>
{{end}}
</table>
`))
homeCreate = template.Must(template.ParseFS(embeddedFS, "home.html"))
}
func linkPath(short string) string {
+17
View File
@@ -0,0 +1,17 @@
<html>
<body>
<h1>go/</h1>
shortlink service.
<h2>create</h2>
<form method="POST" action="/">
http://go/<input name=short size=20 value="{{.Short}}"> ==&gt; <input name=long size=40> <input type=submit value="create">
</form>
<h2>recent popular links</h2>
<table>
<tr><th>short link</th><th>num clicks</th></tr>
{{range .Clicks}}
<tr><td><a href="http://go/{{.Short}}">http://go/{{.Short}}</a></td><td>{{.NumClicks}}</td></tr>
{{end}}
</table>