Skip to content

Commit 9d2c372

Browse files
mullenderwillnorris
andcommitted
add support for browser bar search keyword
Respect custom hostname in open search document. Co-authored-by: Will Norris <[email protected]>
1 parent f2105a0 commit 9d2c372

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

golink.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"io/fs"
1818
"io/ioutil"
1919
"log"
20+
"net"
2021
"net/http"
2122
"net/url"
2223
"os"
@@ -32,12 +33,14 @@ import (
3233
"tailscale.com/tsnet"
3334
)
3435

36+
const defaultHostname = "go"
37+
3538
var (
3639
verbose = flag.Bool("verbose", false, "be verbose")
3740
sqlitefile = flag.String("sqlitedb", "", "path of SQLite database to store links")
3841
dev = flag.String("dev-listen", "", "if non-empty, listen on this addr and run in dev mode; auto-set sqlitedb if empty and don't use tsnet")
3942
snapshot = flag.String("snapshot", "", "file path of snapshot file")
40-
hostname = flag.String("hostname", "go", "service name")
43+
hostname = flag.String("hostname", defaultHostname, "service name")
4144
)
4245

4346
var stats struct {
@@ -52,7 +55,7 @@ var stats struct {
5255
// that will be loaded on startup.
5356
var LastSnapshot []byte
5457

55-
//go:embed static tmpl/*.html
58+
//go:embed static tmpl/*.html tmpl/*.xml
5659
var embeddedFS embed.FS
5760

5861
// db stores short links.
@@ -106,9 +109,20 @@ func Run() error {
106109
http.HandleFunc("/.detail/", serveDetail)
107110
http.HandleFunc("/.export", serveExport)
108111
http.HandleFunc("/.help", serveHelp)
112+
http.HandleFunc("/.opensearch", serveOpenSearch)
109113
http.Handle("/.static/", http.StripPrefix("/.", http.FileServer(http.FS(embeddedFS))))
110114

111115
if *dev != "" {
116+
// override default hostname for dev mode
117+
if *hostname == defaultHostname {
118+
if h, p, err := net.SplitHostPort(*dev); err == nil {
119+
if h == "" {
120+
h = "localhost"
121+
}
122+
*hostname = fmt.Sprintf("%s:%s", h, p)
123+
}
124+
}
125+
112126
log.Printf("Running in dev mode on %s ...", *dev)
113127
log.Fatal(http.ListenAndServe(*dev, nil))
114128
}
@@ -154,6 +168,9 @@ var (
154168

155169
// helpTmpl is the template used by the http://go/.help page
156170
helpTmpl *template.Template
171+
172+
// opensearchTmpl is the template used by the http://go/.opensearch page
173+
opensearchTmpl *template.Template
157174
)
158175

159176
type visitData struct {
@@ -172,6 +189,7 @@ func init() {
172189
detailTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/detail.html"))
173190
successTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/success.html"))
174191
helpTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/base.html", "tmpl/help.html"))
192+
opensearchTmpl = template.Must(template.ParseFS(embeddedFS, "tmpl/opensearch.xml"))
175193
}
176194

177195
// initStats initializes the in-memory stats counter with counts from db.
@@ -244,6 +262,15 @@ func serveHelp(w http.ResponseWriter, _ *http.Request) {
244262
helpTmpl.Execute(w, nil)
245263
}
246264

265+
func serveOpenSearch(w http.ResponseWriter, _ *http.Request) {
266+
type opensearchData struct {
267+
Hostname string
268+
}
269+
270+
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
271+
opensearchTmpl.Execute(w, opensearchData{Hostname: *hostname})
272+
}
273+
247274
func serveGo(w http.ResponseWriter, r *http.Request) {
248275
if r.RequestURI == "/" {
249276
switch r.Method {

static/opensearch.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
2+
<ShortName>localhost</ShortName>
3+
<Description>localhost url redirector</Description>
4+
<InputEncoding>UTF-8</InputEncoding>
5+
<Image width="16" height="16" type="image/x-icon">https://github.com/favicon.ico</Image>
6+
<Url type="text/html" method="get" template="http://localhost:8080/{searchTerms}"/>
7+
<moz:SearchForm>http://localhost:8080/</moz:SearchForm>
8+
</OpenSearchDescription>

tmpl/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<title>go/</title>
55
<link rel="stylesheet" href="/.static/base.css">
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="search" type="application/opensearchdescription+xml" title="searchTitle" href="/.opensearch" />
78
</head>
89
<body class="flex flex-col min-h-screen">
910
<div class="bg-gray-100 border-b border-gray-200 pt-4 pb-2 mb-6">

tmpl/opensearch.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
2+
<ShortName>{{.Hostname}}</ShortName>
3+
<Description>Private shortlinks on your tailnet</Description>
4+
<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>
8+
</OpenSearchDescription>

0 commit comments

Comments
 (0)