Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest long URL for new go links if peer exists #115

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type visitData struct {
// homeData is the data used by the homeTmpl template.
type homeData struct {
Short string
Long string
Clicks []visitData
}

Expand Down Expand Up @@ -379,7 +380,7 @@ func serveHandler() http.Handler {
})
}

func serveHome(w http.ResponseWriter, short string) {
func serveHome(w http.ResponseWriter, r *http.Request, short string) {
var clicks []visitData

stats.mu.Lock()
Expand All @@ -401,8 +402,23 @@ func serveHome(w http.ResponseWriter, short string) {
clicks = clicks[:200]
}

var long string
if short != "" && localClient != nil {
// if a peer exists with the short name, suggest it as the long URL
st, err := localClient.Status(r.Context())
if err == nil {
for _, p := range st.Peer {
if host, _, ok := strings.Cut(p.DNSName, "."); ok && host == short {
long = "http://" + host + "/"
break
}
}
}
}

homeTmpl.Execute(w, homeData{
Short: short,
Long: long,
Clicks: clicks,
})
}
Expand Down Expand Up @@ -442,7 +458,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
switch r.Method {
case "GET":
serveHome(w, "")
serveHome(w, r, "")
case "POST":
serveSave(w, r)
}
Expand All @@ -460,7 +476,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
w.WriteHeader(http.StatusNotFound)
serveHome(w, short)
serveHome(w, r, short)
return
}
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion tmpl/home.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{{ define "main" }}
<h2 class="text-xl font-bold pb-2">Create a new link</h2>

{{ with .Long }}
<p class="">Did you mean <a class="text-blue-600 hover:underline" href="{{.}}">{{.}}</a> ? Create a go link for it now:</p>
{{ end }}
<form method="POST" action="/" class="flex flex-wrap">
<div class="flex">
<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>
<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."
class="p-2 my-2 rounded-r-md border-gray-300 placeholder:text-gray-400">
<span class="flex m-2 items-center">&rarr;</span>
</div>
<input name=long required type=text size=40 placeholder="https://destination-url"{{if .Short}} autofocus{{end}} class="p-2 my-2 mr-2 max-w-full rounded-md border-gray-300 placeholder:text-gray-400">
<input name=long required type=text size=40 placeholder="https://destination-url"{{if .Short}} value="{{.Long}}" autofocus{{end}} class="p-2 my-2 mr-2 max-w-full rounded-md border-gray-300 placeholder:text-gray-400">
<button type=submit class="py-2 px-4 my-2 rounded-md bg-blue-500 border-blue-500 text-white hover:bg-blue-600 hover:border-blue-600">Create</button>
</form>
<p class="text-sm text-gray-500"><a class="text-blue-600 hover:underline" href="/.help">Help and advanced options</a></p>
Expand Down
Loading