Skip to content

Commit a83ef33

Browse files
Use custom hostname in all html templates
Signed-off-by: Will Norris <[email protected]> Co-authored-by: Will Norris <[email protected]>
1 parent 2ca2d9d commit a83ef33

File tree

9 files changed

+69
-51
lines changed

9 files changed

+69
-51
lines changed

golink.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,41 @@ type deleteData struct {
285285
var xsrfKey string
286286

287287
func init() {
288-
homeTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/home.html"))
289-
detailTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/detail.html"))
290-
successTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/success.html"))
291-
helpTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/help.html"))
292-
allTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/all.html"))
293-
deleteTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/delete.html"))
294-
opensearchTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/opensearch.xml"))
288+
homeTmpl = newTemplate("base.html", "home.html")
289+
detailTmpl = newTemplate("base.html", "detail.html")
290+
successTmpl = newTemplate("base.html", "success.html")
291+
helpTmpl = newTemplate("base.html", "help.html")
292+
allTmpl = newTemplate("base.html", "all.html")
293+
deleteTmpl = newTemplate("base.html", "delete.html")
294+
opensearchTmpl = newTemplate("opensearch.xml")
295295

296296
b := make([]byte, 24)
297297
rand.Read(b)
298298
xsrfKey = base64.StdEncoding.EncodeToString(b)
299299
}
300300

301+
var tmplFuncs = template.FuncMap{
302+
"go": func() string {
303+
return *hostname
304+
},
305+
}
306+
307+
// newTemplate creates a new template with the specified files in the tmpl directory.
308+
// The first file name is used as the template name,
309+
// and tmplFuncs are registered as available funcs.
310+
// This func panics if unable to parse files.
311+
func newTemplate(files ...string) *template.Template {
312+
if len(files) == 0 {
313+
return nil
314+
}
315+
tf := make([]string, 0, len(files))
316+
for _, f := range files {
317+
tf = append(tf, "tmpl/"+f)
318+
}
319+
t := template.New(files[0]).Funcs(tmplFuncs)
320+
return template.Must(t.ParseFS(embeddedFS, tf...))
321+
}
322+
301323
// initStats initializes the in-memory stats counter with counts from db.
302324
func initStats() error {
303325
stats.mu.Lock()
@@ -473,12 +495,8 @@ func serveHelp(w http.ResponseWriter, _ *http.Request) {
473495
}
474496

475497
func serveOpenSearch(w http.ResponseWriter, _ *http.Request) {
476-
type opensearchData struct {
477-
Hostname string
478-
}
479-
480498
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
481-
opensearchTmpl.Execute(w, opensearchData{Hostname: *hostname})
499+
opensearchTmpl.Execute(w, nil)
482500
}
483501

484502
func serveGo(w http.ResponseWriter, r *http.Request) {

tmpl/all.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ <h2 class="text-xl font-bold pt-6 pb-2">All Links ({{ len . }} total)</h2>
1313
<tr class="flex hover:bg-gray-100 group border-b border-gray-200">
1414
<td class="flex-1 p-2">
1515
<div class="flex">
16-
<a class="flex-1 hover:text-blue-500 hover:underline" href="/{{ .Short }}">go/{{ .Short }}</a>
16+
<a class="flex-1 hover:text-blue-500 hover:underline" href="/{{ .Short }}">{{go}}/{{ .Short }}</a>
1717
<a class="flex items-center px-2 invisible group-hover:visible" title="Link Details" href="/.detail/{{ .Short }}">
1818
<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>
1919
</a>

tmpl/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4-
<title>go/</title>
4+
<title>{{go}}/</title>
55
<link rel="stylesheet" href="/.static/base.css">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
77
<link rel="search" type="application/opensearchdescription+xml" title="searchTitle" href="/.opensearch" />
@@ -11,7 +11,7 @@
1111
<body class="flex flex-col min-h-screen">
1212
<div class="bg-gray-100 border-b border-gray-200 pt-4 pb-2 mb-6">
1313
<header class="container mx-auto px-4">
14-
<h1 class="text-2xl font-bold pb-1"><a href="/">go/</a></h1>
14+
<h1 class="text-2xl font-bold pb-1"><a href="/">{{go}}/</a></h1>
1515
A private shortlink service for your tailnet
1616
</header>
1717
</div>

tmpl/delete.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{{ define "main" }}
2-
<h2 class="text-xl font-bold pb-2">Link go/{{.Short}} Deleted</h2>
2+
<h2 class="text-xl font-bold pb-2">Link {{go}}/{{.Short}} Deleted</h2>
33

44
<p class="py-4">Deleted this by mistake? You can recreate the same link below.</p>
55

66
<form method="POST" action="/">
77
<input type="hidden" name="xsrf" value="{{ .XSRF }}" />
88
<div class="flex flex-wrap">
99
<div class="flex">
10-
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://go/</label>
10+
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://{{go}}/</label>
1111
<input id=short name=short required type=text size=15 placeholder="shortname" value="{{.Short}}" pattern="\w[\w\-\.]*" title="Must start with letter or number; may contain letters, numbers, dashes, and periods."
1212
class="p-2 my-2 rounded-r-md border-gray-300 placeholder:text-gray-400 disabled:bg-gray-100">
1313
<span class="flex m-2 items-center">&rarr;</span>

tmpl/detail.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ <h2 class="text-xl font-bold pb-2">Link Details</h2>
66
<input type="hidden" name="xsrf" value="{{ .XSRF }}" />
77
<div class="flex flex-wrap">
88
<div class="flex">
9-
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://go/</label>
9+
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://{{go}}/</label>
1010
<input id=short name=short required type=text size=15 placeholder="shortname" value="{{.Link.Short}}" pattern="\w[\w\-\.]*" title="Must start with letter or number; may contain letters, numbers, dashes, and periods."
1111
class="p-2 my-2 rounded-r-md border-gray-300 placeholder:text-gray-400 disabled:bg-gray-100">
1212
<span class="flex m-2 items-center">&rarr;</span>
@@ -40,7 +40,7 @@ <h3 class="text-lg font-bold pb-2 pt-4 text-red-500">Danger Zone</h3>
4040
{{ else }}
4141
<dl>
4242
<dt class="text-sm font-bold mt-6">Name</dt>
43-
<dd><a class="text-blue-600 hover:underline" href="/{{.Link.Short}}">go/{{.Link.Short}}</a></dd>
43+
<dd><a class="text-blue-600 hover:underline" href="/{{.Link.Short}}">{{go}}/{{.Link.Short}}</a></dd>
4444

4545
<dt class="text-sm font-bold mt-6">Destination</dt>
4646
<dd>{{.Link.Long}}</dd>

tmpl/help.html

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{{ define "main" }}
22
<article class="prose max-w-5xl">
33
<p>
4-
go links provide short, memorable links for the websites you and your team use most.
4+
{{go}} links provide short, memorable links for the websites you and your team use most.
55

6-
<h2>Creating go links</h2>
6+
<h2>Creating {{go}} links</h2>
77

88
<p>
9-
All go links have a <strong>short name</strong> and a <strong>destination link</strong> that the go link points to.
9+
All {{go}} links have a <strong>short name</strong> and a <strong>destination link</strong> that the {{go}} link points to.
1010
Some notes on short names:
1111

1212
<ul>
1313
<li>names must start with a letter or number
1414
<li>names may contain letters, numbers, hyphens, and periods
15-
<li>names are <strong>not</strong> case-sensitive (go/foo is the same as go/FOO)
16-
<li>hyphens are ignored when resolving links (go/meetingnotes is the same as go/meeting-notes)
15+
<li>names are <strong>not</strong> case-sensitive ({{go}}/foo is the same as {{go}}/FOO)
16+
<li>hyphens are ignored when resolving links ({{go}}/meetingnotes is the same as {{go}}/meeting-notes)
1717
</ul>
1818

1919
<p>
@@ -25,26 +25,26 @@ <h2>Creating go links</h2>
2525
<h2>Resolving links</h2>
2626

2727
<p>
28-
When logged in to your Tailscale network, go links can be entered directly into any browser or command line utility such as curl.
28+
When logged in to your Tailscale network, {{go}} links can be entered directly into any browser or command line utility such as curl.
2929
You do not need any additional browser extensions.
3030

3131
<p>
3232
Any additional path provided after the short name will be added to the end of the destination link.
33-
For example, if <strong>go/who</strong> goes to your company directory at <strong>http://directory/</strong>,
34-
then <strong>go/who/amelie</strong> will go to <strong>http://directory/amelie</strong>.
33+
For example, if <strong>{{go}}/who</strong> goes to your company directory at <strong>http://directory/</strong>,
34+
then <strong>{{go}}/who/amelie</strong> will go to <strong>http://directory/amelie</strong>.
3535

3636
<p>
3737
<a href="#advanced">Advanced destination links</a> allow you to further customize this behavior.
3838

3939
<h2 id="advanced">Advanced destination links</h2>
4040

4141
<p>
42-
To have more control over how go links are resolved, destination links can use <a href="https://pkg.go.dev/text/template">Go template syntax</a>.
42+
To have more control over how {{go}} links are resolved, destination links can use <a href="https://pkg.go.dev/text/template">Go template syntax</a>.
4343
Templates are provided a data structure with the following fields:
4444

4545
<ul>
4646
<li><code>.Path</code> is the remaining path value after the short name (without a leading slash).
47-
For the link <strong>go/who/amelie</strong>, the value of <code>.Path</code> is <code>amelie</code>.
47+
For the link <strong>{{go}}/who/amelie</strong>, the value of <code>.Path</code> is <code>amelie</code>.
4848
<li><code>.Now</code> is a <a href="https://pkg.go.dev/time#Time">time.Time</a> value representing the current date and time.
4949
<li><code>.User</code> is the current user resolving the link.
5050
This is the email address of the user or <code>{username}@github</code> for tailnets that use GitHub authentication.
@@ -61,34 +61,34 @@ <h2 id="advanced">Advanced destination links</h2>
6161

6262
<p>
6363
The most common use of advanced destination links is to put the additional path in a custom location in the destination link.
64-
For example, you might set the destination for <strong>go/search</strong> to:
64+
For example, you might set the destination for <strong>{{go}}/search</strong> to:
6565

6666
<pre>{{`https://www.google.com/{{if .Path}}search?q={{QueryEscape .Path}}{{end}}`}}</pre>
6767

68-
When a user visits <strong>go/search</strong> with no additional path, they will be directed to <a href="https://www.google.com/">https://www.google.com/</a>.
69-
If they include an additional path like <strong>go/search/pangolins</strong>, they will be directed to <a href="https://www.google.com/search?q=pangolins">https://www.google.com/search?q=pangolins</a>.
68+
When a user visits <strong>{{go}}/search</strong> with no additional path, they will be directed to <a href="https://www.google.com/">https://www.google.com/</a>.
69+
If they include an additional path like <strong>{{go}}/search/pangolins</strong>, they will be directed to <a href="https://www.google.com/search?q=pangolins">https://www.google.com/search?q=pangolins</a>.
7070

7171
<h3>Examples</h3>
7272

7373
<table>
7474
<tr>
7575
<td>Include path in query</td>
76-
<td>go/search</td>
76+
<td>{{go}}/search</td>
7777
<td>{{`https://cloudsearch.google.com/{{if .Path}}cloudsearch/search?q={{QueryEscape .Path}}{{end}}`}}</td>
7878
</tr>
7979
<tr>
8080
<td>Include path in destination path</td>
81-
<td>go/slack</td>
81+
<td>{{go}}/slack</td>
8282
<td>{{`https://company.slack.com/{{if .Path}}channels/{{PathEscape .Path}}{{end}}`}}</td>
8383
</tr>
8484
<tr>
8585
<td>Include path in hostname</td>
86-
<td>go/varz</td>
86+
<td>{{go}}/varz</td>
8787
<td>{{`http://{{if .Path}}{{.Path}}{{else}}host{{end}}.example/debug/varz`}}</td>
8888
</tr>
8989
<tr>
9090
<td>Include today's date in wiki page</td>
91-
<td>go/today</td>
91+
<td>{{go}}/today</td>
9292
<td>{{`http://wiki/{{.Now.Format "01-02-2006"}}`}}</td>
9393
</tr>
9494
</table>
@@ -101,8 +101,8 @@ <h2 id="api">Application Programming Interface (API)</h2>
101101
<p>
102102
Include a "+" after a link to get information about a link without resolving it:
103103

104-
<pre>{{`$ curl -L go/search+
105-
{
104+
<pre>$ curl -L {{go}}/search+
105+
{{`{
106106
"Short": "search",
107107
"Long": "https://cloudsearch.google.com/{{if .Path}}cloudsearch/search?q={{QueryEscape .Path}}{{end}}",
108108
"Created": "2022-06-08T04:27:32.829906577Z",
@@ -113,19 +113,19 @@ <h2 id="api">Application Programming Interface (API)</h2>
113113
</pre>
114114

115115
<p>
116-
Visit <a href="/.export">go/.export</a> to export all saved links and their metadata in <a href="https://jsonlines.org/">JSON Lines format</a>.
116+
Visit <a href="/.export">{{go}}/.export</a> to export all saved links and their metadata in <a href="https://jsonlines.org/">JSON Lines format</a>.
117117
This is useful to create data snapshots that can be restored later.
118118

119-
<pre>{{`$ curl -L go/.export
120-
{"Short":"go","Long":"http://go","Created":"2022-05-31T13:04:44.741457796-07:00","LastEdit":"2022-05-31T13:04:44.741457796-07:00","Owner":"[email protected]","Clicks":1}
119+
<pre>$ curl -L {{go}}/.export
120+
{{`{"Short":"go","Long":"http://go","Created":"2022-05-31T13:04:44.741457796-07:00","LastEdit":"2022-05-31T13:04:44.741457796-07:00","Owner":"[email protected]","Clicks":1}
121121
{"Short":"slack","Long":"https://company.slack.com/{{if .Path}}channels/{{PathEscape .Path}}{{end}}","Created":"2022-06-17T18:05:43.562948451Z","LastEdit":"2022-06-17T18:06:35.811398Z","Owner":"[email protected]","Clicks":4}`}}
122122
</pre>
123123

124124
<p>
125125
Create a new link by sending a POST request with a <code>short</code> and <code>long</code> value:
126126

127-
<pre>{{`$ curl -L -H Sec-Golink:1 -d short=cs -d long=https://cs.github.com/ go
128-
{"Short":"cs","Long":"https://cs.github.com/","Created":"2022-06-03T22:15:29.993978392Z","LastEdit":"2022-06-03T22:15:29.993978392Z","Owner":"[email protected]"}`}}
127+
<pre>$ curl -L -H Sec-Golink:1 -d short=cs -d long=https://cs.github.com/ {{go}}
128+
{{`{"Short":"cs","Long":"https://cs.github.com/","Created":"2022-06-03T22:15:29.993978392Z","LastEdit":"2022-06-03T22:15:29.993978392Z","Owner":"[email protected]"}`}}
129129
</pre>
130130

131131
</article>

tmpl/home.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
<h2 class="text-xl font-bold pb-2">Create a new link</h2>
33

44
{{ with .Long }}
5-
<p class="">Did you mean <a class="text-blue-600 hover:underline" href="{{.}}">{{.}}</a> ? Create a go link for it now:</p>
5+
<p class="">Did you mean <a class="text-blue-600 hover:underline" href="{{.}}">{{.}}</a> ? Create a {{go}} link for it now:</p>
66
{{ end }}
77
<form method="POST" action="/" class="flex flex-wrap">
88
<input type="hidden" name="xsrf" value="{{ .XSRF }}" />
99
<div class="flex">
10-
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://go/</label>
10+
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://{{go}}/</label>
1111
<input id=short name=short required type=text size=15 placeholder="shortname" value="{{.Short}}" pattern="\w[\w\-\.]*" title="Must start with letter or number; may contain letters, numbers, dashes, and periods."
1212
class="p-2 my-2 rounded-r-md border-gray-300 placeholder:text-gray-400">
1313
<span class="flex m-2 items-center">&rarr;</span>
@@ -29,7 +29,7 @@ <h2 class="text-xl font-bold pt-6 pb-2">Popular Links</h2>
2929
{{range .Clicks}}
3030
<tr class="hover:bg-gray-100 group border-b border-gray-200">
3131
<td class="flex">
32-
<a class="block flex-1 p-2 pr-4 hover:text-blue-500 hover:underline" href="/{{.Short}}">go/{{.Short}}</a>
32+
<a class="block flex-1 p-2 pr-4 hover:text-blue-500 hover:underline" href="/{{.Short}}">{{go}}/{{.Short}}</a>
3333
<a class="flex items-center px-2 invisible group-hover:visible" title="Link Details" href="/.detail/{{.Short}}">
3434
<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>
3535
</a>

tmpl/opensearch.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
2-
<ShortName>{{.Hostname}}</ShortName>
2+
<ShortName>{{go}}</ShortName>
33
<Description>Private shortlinks on your tailnet</Description>
44
<InputEncoding>UTF-8</InputEncoding>
5-
<Image width="16" height="16" type="image/png">http://{{.Hostname}}/.static/favicon.png</Image>
6-
<Url type="text/html" method="get" template="http://{{.Hostname}}/{searchTerms}"/>
7-
<moz:SearchForm>http://{{.Hostname}}/</moz:SearchForm>
5+
<Image width="16" height="16" type="image/png">http://{{go}}/.static/favicon.png</Image>
6+
<Url type="text/html" method="get" template="http://{{go}}/{searchTerms}"/>
7+
<moz:SearchForm>http://{{go}}/</moz:SearchForm>
88
</OpenSearchDescription>

tmpl/success.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{ define "main" }}
22
<h2 class="text-xl font-bold pb-2">Success</h2>
33

4-
<p><a class="text-blue-600 hover:underline" href="/{{.Short}}">go/{{.Short}}</a> has been saved.</p>
4+
<p><a class="text-blue-600 hover:underline" href="/{{.Short}}">{{go}}/{{.Short}}</a> has been saved.</p>
55
{{ end }}

0 commit comments

Comments
 (0)