diff --git a/db.go b/db.go index 451bd7c..1aec46b 100644 --- a/db.go +++ b/db.go @@ -11,10 +11,12 @@ import ( "fmt" "io/fs" "net/url" + "sort" "strings" "sync" "time" + "github.com/sahilm/fuzzy" _ "modernc.org/sqlite" "tailscale.com/tstime" ) @@ -40,13 +42,46 @@ func linkID(short string) string { } // SQLiteDB stores Links in a SQLite database. +// +// It also maintains an in-memory index of all Links, kept in sync with the +// database under the single mutex below. The index exists to service +// low-latency fuzzy autocomplete queries ([SQLiteDB.SearchShort]) without +// issuing a SQL query per keystroke. type SQLiteDB struct { db *sql.DB mu sync.RWMutex + // idx is the in-memory index of all Links, keyed by linkID(Short). + // It is populated on construction from LoadAll and kept in sync by + // Save, SaveAll, and Delete. Link values in idx are never mutated + // after insertion (callers of Save construct a fresh *Link each time), + // so pointers may be read without copying. + idx map[string]*Link + clock tstime.Clock // allow overriding time for tests } +// SearchMatch is a single link that matched a SearchShort query, +// along with information useful for highlighting and ranking. +type SearchMatch struct { + Link *Link + // ShortMatchedIndexes are the rune indexes within Link.Short that + // matched characters from the query, or nil if the query did not + // match against the short name. + ShortMatchedIndexes []int + // LongMatchedIndexes are the rune indexes within Link.Long that + // matched characters from the query, populated only in includeLong + // mode when the query matched the long URL, or nil otherwise. + LongMatchedIndexes []int + // Score is the best (highest) fuzzy score across fields for this + // candidate. Higher is better. + Score int + // MatchedShort is true if the query matched against Link.Short. + // If MatchedShort is false (includeLong mode only), the match came + // from Link.Long alone. + MatchedShort bool +} + //go:embed schema.sql var sqlSchema string @@ -64,7 +99,35 @@ func NewSQLiteDB(f string) (*SQLiteDB, error) { return nil, err } - return &SQLiteDB{db: db}, nil + s := &SQLiteDB{db: db, idx: make(map[string]*Link)} + // Populate the in-memory index from any pre-existing rows. + if err := s.rebuildIndexLocked(); err != nil { + return nil, fmt.Errorf("rebuilding in-memory link index: %w", err) + } + return s, nil +} + +// rebuildIndexLocked populates s.idx from the Links table. It must be called +// with s.mu held for writing (or with no concurrent users, as in the +// constructor). We bypass LoadAll to avoid deadlocking on the mutex. +func (s *SQLiteDB) rebuildIndexLocked() error { + rows, err := s.db.Query("SELECT Short, Long, Created, LastEdit, Owner FROM Links") + if err != nil { + return err + } + defer rows.Close() + s.idx = make(map[string]*Link) + for rows.Next() { + link := new(Link) + var created, lastEdit int64 + if err := rows.Scan(&link.Short, &link.Long, &created, &lastEdit, &link.Owner); err != nil { + return err + } + link.Created = time.Unix(created, 0).UTC() + link.LastEdit = time.Unix(lastEdit, 0).UTC() + s.idx[linkID(link.Short)] = link + } + return rows.Err() } // Now returns the current time. @@ -138,6 +201,10 @@ func (s *SQLiteDB) Save(link *Link) error { if rows != 1 { return fmt.Errorf("expected to affect 1 row, affected %d", rows) } + // Keep the in-memory index in sync. Store a copy so the caller + // can't mutate the indexed value after the fact. + cp := *link + s.idx[linkID(link.Short)] = &cp return nil } @@ -159,24 +226,38 @@ func (s *SQLiteDB) SaveAbsent(links []*Link) (int, error) { } defer stmt.Close() - var inserted int + // Track which links were actually inserted (vs ignored) so we can + // mirror the DB state into the in-memory index only after a + // successful Commit. + insertedLinks := make([]*Link, 0, len(links)) for _, link := range links { result, err := stmt.Exec(linkID(link.Short), link.Short, link.Long, link.Created.Unix(), link.LastEdit.Unix(), link.Owner) if err != nil { tx.Rollback() - return inserted, err + return len(insertedLinks), err } rows, err := result.RowsAffected() if err != nil { tx.Rollback() - return inserted, err + return len(insertedLinks), err + } + if rows == 1 { + insertedLinks = append(insertedLinks, link) } - inserted += int(rows) } if err := tx.Commit(); err != nil { return 0, err } - return inserted, nil + for _, link := range insertedLinks { + // Only insert if absent so we don't stomp an entry that the DB + // had already (INSERT OR IGNORE keeps the existing row). + id := linkID(link.Short) + if _, exists := s.idx[id]; !exists { + cp := *link + s.idx[id] = &cp + } + } + return len(insertedLinks), nil } // Delete removes a Link using its short name. @@ -195,6 +276,7 @@ func (s *SQLiteDB) Delete(short string) error { if rows != 1 { return fmt.Errorf("expected to affect 1 row, affected %d", rows) } + delete(s.idx, linkID(short)) return nil } @@ -264,26 +346,143 @@ func (s *SQLiteDB) DeleteStats(short string) error { return nil } -// GetLinksByOwner returns all Links owned by the specified owner. -func (s *SQLiteDB) GetLinksByOwner(owner string) ([]*Link, error) { +// SearchShort returns up to `limit` links whose short name (and, if +// includeLong is true, whose long URL too) fuzzy-match the given query, +// restricted to links satisfying filters. Results are ordered best-match first. +// +// Matching is powered by github.com/sahilm/fuzzy (fzf-style subsequence +// matching with bonuses for matches at the start of the string, following +// separators such as "-" "." "_", and adjacent to previous matches). +// +// In includeLong mode, each link is scored twice (once against Short, once +// against Long) and receives the higher of the two scores; the returned +// SearchMatch carries both ShortMatchedIndexes and LongMatchedIndexes so +// callers can highlight matches in whichever field matched. +// +// When query is empty but filters is non-empty, no fuzzy matching is performed; +// every link passing the filter is returned, ordered alphabetically by short +// name. When both query and filters are empty, the result is empty. +// +// Searches run entirely against the in-memory index; no SQL is issued. +func (s *SQLiteDB) SearchShort(query string, includeLong bool, filters searchFilters, limit int) []SearchMatch { + if limit <= 0 || (query == "" && filters.Empty()) { + return nil + } s.mu.RLock() defer s.mu.RUnlock() - var links []*Link - rows, err := s.db.Query("SELECT Short, Long, Created, LastEdit, Owner FROM Links WHERE LOWER(Owner) = LOWER(?)", owner) - if err != nil { - return nil, err + // Build a stable ordering of filtered candidates so that ties break + // predictably (alphabetical by short name). + links := make([]*Link, 0, len(s.idx)) + for _, l := range s.idx { + if filters.Owner != "" && !strings.EqualFold(l.Owner, filters.Owner) { + continue + } + links = append(links, l) } - for rows.Next() { - link := new(Link) - var created, lastEdit int64 - err := rows.Scan(&link.Short, &link.Long, &created, &lastEdit, &link.Owner) - if err != nil { - return nil, err + sort.Slice(links, func(i, j int) bool { + return links[i].Short < links[j].Short + }) + + // A filter with no query is a plain listing: the candidates are already + // sorted, so just cap and return them with no match highlighting. + if query == "" { + if len(links) > limit { + links = links[:limit] } - link.Created = time.Unix(created, 0).UTC() - link.LastEdit = time.Unix(lastEdit, 0).UTC() - links = append(links, link) + out := make([]SearchMatch, 0, len(links)) + for _, l := range links { + out = append(out, SearchMatch{Link: l}) + } + return out } - return links, rows.Err() + + // Pass 1: fuzzy match against short names. + shortMatches := fuzzy.FindFrom(query, linkShortSource(links)) + + // When includeLong is false, we're done after pass 1. + if !includeLong { + if len(shortMatches) > limit { + shortMatches = shortMatches[:limit] + } + out := make([]SearchMatch, 0, len(shortMatches)) + for _, m := range shortMatches { + out = append(out, SearchMatch{ + Link: links[m.Index], + ShortMatchedIndexes: m.MatchedIndexes, + Score: m.Score, + MatchedShort: true, + }) + } + return out + } + + // Pass 2 (includeLong): fuzzy match against long URLs. Merge with + // pass 1 by link index, keeping the best score and remembering + // which field(s) matched. We run two separate passes rather than + // concatenating the strings because sahilm/fuzzy treats a NUL byte + // (0) as an end-of-string sentinel internally, so any composite + // separator would either be stripped out of candidates or confuse + // the matcher. + merged := make(map[int]*SearchMatch, len(shortMatches)) + for _, m := range shortMatches { + merged[m.Index] = &SearchMatch{ + Link: links[m.Index], + ShortMatchedIndexes: m.MatchedIndexes, + Score: m.Score, + MatchedShort: true, + } + } + longMatches := fuzzy.FindFrom(query, linkLongSource(links)) + for _, m := range longMatches { + if existing, ok := merged[m.Index]; ok { + existing.LongMatchedIndexes = m.MatchedIndexes + // Keep the better score; if the long score is higher + // than the short score, the long match "wins" but we + // still keep both sets of positions so either field + // can be highlighted. + if m.Score > existing.Score { + existing.Score = m.Score + } + } else { + merged[m.Index] = &SearchMatch{ + Link: links[m.Index], + LongMatchedIndexes: m.MatchedIndexes, + Score: m.Score, + MatchedShort: false, + } + } + } + // Sort merged results by score desc, then by short name asc for + // a stable tie-break. + all := make([]*SearchMatch, 0, len(merged)) + for _, m := range merged { + all = append(all, m) + } + sort.Slice(all, func(i, j int) bool { + if all[i].Score != all[j].Score { + return all[i].Score > all[j].Score + } + return all[i].Link.Short < all[j].Link.Short + }) + if len(all) > limit { + all = all[:limit] + } + out := make([]SearchMatch, len(all)) + for i, m := range all { + out[i] = *m + } + return out } + +// linkShortSource and linkLongSource adapt a []*Link into the fuzzy.Source +// interface, exposing either the Short or Long field for matching. +type linkShortSource []*Link + +func (s linkShortSource) Len() int { return len(s) } +func (s linkShortSource) String(i int) string { return s[i].Short } + +type linkLongSource []*Link + +func (s linkLongSource) Len() int { return len(s) } +func (s linkLongSource) String(i int) string { return s[i].Long } diff --git a/db_test.go b/db_test.go index aad7b28..f0c21d0 100644 --- a/db_test.go +++ b/db_test.go @@ -126,42 +126,234 @@ func Test_SQLiteDB_SaveLoadDeleteStats(t *testing.T) { } } -// Test GetLinksByOwner functionality -func Test_SQLiteDB_GetLinksByOwner(t *testing.T) { +// Test_SQLiteDB_IndexStaysInSync verifies that Save, SaveAll, and Delete +// keep the in-memory link index consistent with the SQL table. +func Test_SQLiteDB_IndexStaysInSync(t *testing.T) { db, err := NewSQLiteDB(path.Join(t.TempDir(), "links.db")) if err != nil { - t.Error(err) + t.Fatal(err) } - // preload some links with owner - links := []*Link{ - {Short: "a", Owner: "foo@bar.com"}, - {Short: "B-c", Owner: "bar@foo.com "}, - } - for _, link := range links { - if err := db.Save(link); err != nil { - t.Error(err) + // snapshot compares the in-memory idx against a fresh LoadAll of + // the SQLite table, keyed by linkID. Link values are compared by + // Short/Long/Owner (timestamps are truncated to seconds by storage + // and preserved verbatim in the index, so we ignore them here). + snapshot := func(stepName string) { + t.Helper() + got, err := db.LoadAll() + if err != nil { + t.Fatalf("[%s] LoadAll: %v", stepName, err) + } + wantIDs := make(map[string]bool, len(got)) + for _, l := range got { + wantIDs[linkID(l.Short)] = true + } + db.mu.RLock() + defer db.mu.RUnlock() + if len(db.idx) != len(got) { + t.Errorf("[%s] idx size = %d, db rows = %d", stepName, len(db.idx), len(got)) } + for id := range wantIDs { + l, ok := db.idx[id] + if !ok { + t.Errorf("[%s] row with id %q not present in idx", stepName, id) + continue + } + if linkID(l.Short) != id { + t.Errorf("[%s] idx key %q points to link with Short %q (expected linkID to match)", stepName, id, l.Short) + } + } + for id, l := range db.idx { + if !wantIDs[id] { + t.Errorf("[%s] idx contains id %q (%+v) not in DB", stepName, id, l) + } + } + } + + snapshot("empty") + + // Save one. + if err := db.Save(&Link{Short: "foo", Long: "http://foo/"}); err != nil { + t.Fatal(err) } + snapshot("after save foo") - want := []*Link{ - {Short: "a", Owner: "foo@bar.com"}, + // Overwrite it. + if err := db.Save(&Link{Short: "foo", Long: "http://foo2/"}); err != nil { + t.Fatal(err) } - got, err := db.GetLinksByOwner("foo@bar.com") + if l := db.idx[linkID("foo")]; l == nil || l.Long != "http://foo2/" { + t.Errorf("idx foo.Long = %q, want http://foo2/", l.Long) + } + snapshot("after overwrite foo") + + // SaveAbsent with a mix of new and existing links. + n, err := db.SaveAbsent([]*Link{ + {Short: "foo", Long: "http://ignored/"}, // INSERT OR IGNORE: should not overwrite + {Short: "bar", Long: "http://bar/"}, + {Short: "baz", Long: "http://baz/"}, + }) if err != nil { - t.Error(err) + t.Fatal(err) } + if n != 2 { + t.Errorf("SaveAll inserted = %d, want 2", n) + } + if l := db.idx[linkID("foo")]; l == nil || l.Long != "http://foo2/" { + t.Errorf("after SaveAll, idx foo.Long = %q, want unchanged http://foo2/", l.Long) + } + snapshot("after SaveAll") - if !cmp.Equal(got, want) { - t.Errorf("db.GetLinksByOwner got %v; want %v", got, want) + // Delete one. + if err := db.Delete("bar"); err != nil { + t.Fatal(err) } + if _, ok := db.idx[linkID("bar")]; ok { + t.Errorf("idx still contains bar after Delete") + } + snapshot("after delete bar") - // confirm empty response for non-existant owner - got, err = db.GetLinksByOwner("foo1@bar.com") + // Caller mutates the link struct after Save: the idx entry must + // not change (we store a copy). + orig := &Link{Short: "mut", Long: "http://before/"} + if err := db.Save(orig); err != nil { + t.Fatal(err) + } + orig.Long = "http://after/" + if l := db.idx[linkID("mut")]; l == nil || l.Long != "http://before/" { + t.Errorf("idx mut.Long = %v, want http://before/ (caller-side mutation leaked into idx)", l) + } +} + +// Test_SQLiteDB_SearchShort verifies the fuzzy autocomplete ranking. +func Test_SQLiteDB_SearchShort(t *testing.T) { + db, err := NewSQLiteDB(path.Join(t.TempDir(), "links.db")) if err != nil { - t.Error(err) + t.Fatal(err) + } + corpus := []*Link{ + {Short: "foo", Long: "http://foo/"}, + {Short: "foo-bar", Long: "http://foobar/"}, + {Short: "food", Long: "http://food/"}, + {Short: "2023-review", Long: "http://docs/2023"}, + {Short: "team.docs", Long: "http://docs/team"}, + {Short: "unrelated", Long: "http://example/"}, + } + for _, l := range corpus { + if err := db.Save(l); err != nil { + t.Fatal(err) + } + } + + // Empty query yields no matches. + if got := db.SearchShort("", false, searchFilters{}, 10); len(got) != 0 { + t.Errorf("SearchShort empty query got %d results, want 0", len(got)) + } + + // Exact prefix "foo" should rank "foo" first (first-char bonus + length). + got := db.SearchShort("foo", false, searchFilters{}, 10) + if len(got) == 0 || got[0].Link.Short != "foo" { + t.Errorf("SearchShort(foo) top = %v, want foo first; got %d results", firstShort(got), len(got)) + } + + // "fb" should prefer "foo-bar" (separator-boundary bonus on 'b' + // after '-') over other candidates that contain 'f' and 'b'. + got = db.SearchShort("fb", false, searchFilters{}, 10) + if len(got) == 0 || got[0].Link.Short != "foo-bar" { + t.Errorf("SearchShort(fb) top = %v, want foo-bar first", firstShort(got)) + } + + // Matched indexes should be returned and be valid rune offsets. + got = db.SearchShort("fb", false, searchFilters{}, 10) + if len(got) > 0 { + idxs := got[0].ShortMatchedIndexes + if len(idxs) != 2 { + t.Errorf("expected 2 short matched indexes, got %v", idxs) + } + if got[0].LongMatchedIndexes != nil { + t.Errorf("LongMatchedIndexes = %v, want nil (not includeLong)", got[0].LongMatchedIndexes) + } + if !got[0].MatchedShort { + t.Errorf("MatchedShort = false, want true") + } + } + + // Limit respected. + got = db.SearchShort("o", false, searchFilters{}, 2) + if len(got) > 2 { + t.Errorf("SearchShort(o, limit=2) returned %d results, want <=2", len(got)) + } + + // includeLong mode: query that only appears in the Long field + // should produce a match. + got = db.SearchShort("example", true, searchFilters{}, 10) + foundUnrelated := false + for _, m := range got { + if m.Link.Short == "unrelated" { + foundUnrelated = true + break + } + } + if !foundUnrelated { + t.Errorf("SearchShort(example, includeLong=true) did not return unrelated (which matches via Long): %v", allShorts(got)) + } +} + +func firstShort(ms []SearchMatch) string { + if len(ms) == 0 { + return "" + } + return ms[0].Link.Short +} + +func allShorts(ms []SearchMatch) []string { + out := make([]string, 0, len(ms)) + for _, m := range ms { + out = append(out, m.Link.Short) + } + return out +} + +// Test_SQLiteDB_SearchShort_OwnerFilter verifies the owner filter, including +// case-insensitive matching and the filter-only listing (empty query). +func Test_SQLiteDB_SearchShort_OwnerFilter(t *testing.T) { + db, err := NewSQLiteDB(path.Join(t.TempDir(), "links.db")) + if err != nil { + t.Fatal(err) + } + corpus := []*Link{ + {Short: "alpha", Owner: "foo@bar.com"}, + {Short: "alias", Owner: "foo@bar.com"}, + {Short: "beta", Owner: "FOO@BAR.COM"}, + {Short: "gamma", Owner: "other@bar.com"}, + } + for _, l := range corpus { + if err := db.Save(l); err != nil { + t.Fatal(err) + } + } + + // Filter-only (empty query) lists every link owned by the address, + // case-insensitively, sorted by short name. + got := db.SearchShort("", false, searchFilters{Owner: "foo@bar.com"}, 10) + if want := []string{"alias", "alpha", "beta"}; !cmp.Equal(allShorts(got), want) { + t.Errorf("owner filter got %v; want %v", allShorts(got), want) + } + + // An owner with no links yields nothing. + if got := db.SearchShort("", false, searchFilters{Owner: "nobody@bar.com"}, 10); len(got) != 0 { + t.Errorf("unknown owner got %d results, want 0", len(got)) + } + + // Filter + fuzzy text ranks within the owner's links only; "gamma" + // (a different owner) must not appear even though it fuzzy-matches "a". + got = db.SearchShort("al", false, searchFilters{Owner: "foo@bar.com"}, 10) + for _, m := range got { + if m.Link.Owner == "other@bar.com" { + t.Errorf("owner filter leaked a link owned by %q", m.Link.Owner) + } } - if len(got) != 0 { - t.Errorf("db.GetLinksByOwner got %v; want empty slice", got) + if len(got) == 0 { + t.Errorf("owner filter + query returned no results, want matches for al") } } diff --git a/go.mod b/go.mod index c0f054c..4efa3d8 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.26.6 require ( github.com/google/go-cmp v0.7.0 github.com/prometheus/client_golang v1.23.2 + github.com/sahilm/fuzzy v0.1.1 golang.org/x/net v0.57.0 modernc.org/sqlite v1.39.1 tailscale.com v1.102.3 diff --git a/go.mod.sri b/go.mod.sri index b89e743..6e604e8 100644 --- a/go.mod.sri +++ b/go.mod.sri @@ -1 +1 @@ -sha256-o2pQ4CPfkTJtj+ZacAqoNO4QecznM4OTyz2j65HPKrU= \ No newline at end of file +sha256-cQOFB5YXDc/EOBAIZKI5bEanIkXCJ+KXqcT3TrDg8m0= \ No newline at end of file diff --git a/go.sum b/go.sum index 00a39c6..e48852c 100644 --- a/go.sum +++ b/go.sum @@ -174,6 +174,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/safchain/ethtool v0.3.0 h1:gimQJpsI6sc1yIqP/y8GYgiXn/NjgvpM0RNoWLVVmP0= github.com/safchain/ethtool v0.3.0/go.mod h1:SA9BwrgyAqNo7M+uaL6IYbxpm5wk3L7Mm6ocLW+CJUs= +github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= +github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/studio-b12/gowebdav v0.13.0 h1:OcwSg6IQHOFNdYHn3bPOHwSE8looG8N56Y5xTT1asqQ= diff --git a/golink.go b/golink.go index 9a0fe79..08bdf99 100644 --- a/golink.go +++ b/golink.go @@ -15,6 +15,7 @@ import ( "errors" "flag" "fmt" + "html" "html/template" "io/fs" "log" @@ -518,6 +519,16 @@ func (s *Server) searchResults(links []*Link) []searchResult { return results } +// annotateClicks fills in each result's NumClicks from the live in-memory +// counter (the same source the home page uses), under the stats lock. +func (s *Server) annotateClicks(results []SearchResult) { + s.stats.mu.Lock() + defer s.stats.mu.Unlock() + for i := range results { + results[i].NumClicks = s.stats.clicks[results[i].Short] + } +} + // homeData is the data used by homeTmpl. type homeData struct { Short string @@ -551,6 +562,38 @@ func (s *Server) tmplFuncs() template.FuncMap { } return s.hostname }, + // contains reports whether s contains substr. Exposed for templates. + "contains": strings.Contains, + // highlight wraps each matched rune in the given string with a + // tag, returning HTML-safe output. Positions are rune indexes (not + // byte offsets), mirroring what sahilm/fuzzy returns. It's used by + // the search page to emphasize which characters of a short name or + // long URL matched the query, matching the autocomplete dropdown + // rendering. + // + // This function must HTML-escape the characters itself since it + // returns template.HTML to bypass the engine's default escaping of + // the tags. + "highlight": func(s string, positions []int) template.HTML { + if len(positions) == 0 { + return template.HTML(html.EscapeString(s)) + } + pos := make(map[int]bool, len(positions)) + for _, p := range positions { + pos[p] = true + } + var b strings.Builder + for i, r := range []rune(s) { + if pos[i] { + b.WriteString("") + b.WriteString(html.EscapeString(string(r))) + b.WriteString("") + } else { + b.WriteString(html.EscapeString(string(r))) + } + } + return template.HTML(b.String()) + }, } } @@ -769,6 +812,9 @@ func (s *Server) serveHome(w http.ResponseWriter, r *http.Request, short string) }) } +// serveAll handles GET /.all, listing every link in the same table view +// searchTmpl uses for a filter-only search. Unlike /.search it expands no +// templates and takes no query; each result links straight to go/{short}. func (s *Server) serveAll(w http.ResponseWriter, _ *http.Request) { if err := s.FlushStats(); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -781,7 +827,24 @@ func (s *Server) serveAll(w http.ResponseWriter, _ *http.Request) { return } - s.searchTmpl.Execute(w, s.searchResults(links)) + results := make([]SearchResult, 0, len(links)) + for _, l := range links { + results = append(results, SearchResult{ + Short: l.Short, + Long: l.Long, + Rendered: l.Long, + Owner: l.Owner, + LastEdit: l.LastEdit, + ShortEscaped: url.PathEscape(l.Short), + Target: "/" + url.PathEscape(l.Short), + DetailTarget: "/.detail/" + url.PathEscape(l.Short), + }) + } + s.annotateClicks(results) + + if err := s.searchTmpl.Execute(w, searchPageData{All: true, Results: results}); err != nil { + log.Printf("searchTmpl.Execute: %v", err) + } } func (s *Server) serveHelp(w http.ResponseWriter, _ *http.Request) { @@ -934,22 +997,271 @@ func (s *Server) serveDetail(w http.ResponseWriter, r *http.Request) { s.detailTmpl.Execute(w, data) } -// serveSearch handles requests to /.search?q={query}, where {query} can currently only be -// the owner formated like "owner:". +// searchFilters holds the structured filter tokens parsed off the front of a +// search query. A zero searchFilters means no filters were given. +type searchFilters struct { + // Owner, if non-empty, restricts results to links owned by this + // address. It is matched case-insensitively (see [SQLiteDB.SearchShort]). + Owner string +} + +// Empty reports whether no filters are set. +func (f searchFilters) Empty() bool { + return f == searchFilters{} +} + +// searchInput is the parsed form of a raw search query string. +// +// Inputs mirror the redirect path taken by serveGo/resolveLink so that the +// preview shown to the user matches what would actually happen on click. +// +// Examples: +// +// "foo" -> {ShortQuery: "foo"} +// "foo/bar" -> {ShortQuery: "foo", Path: "bar"} +// "foo/bar/baz?x=1" -> {ShortQuery: "foo", Path: "bar/baz", Query: {x: [1]}} +// "?docs" -> {ShortQuery: "docs", IncludeLong: true} +// " ?foo/bar" -> {ShortQuery: "foo", Path: "bar", IncludeLong: true} +// "owner:a@b.com" -> {Filters: {Owner: "a@b.com"}} +// "owner:a@b.com deploy" -> {Filters: {Owner: "a@b.com"}, ShortQuery: "deploy"} +type searchInput struct { + // ShortQuery is the text the fuzzy matcher should match against the + // link short names. + ShortQuery string + // Path is the remaining path after the short name, if any. + // Corresponds to env.Path in [expandLink]. + Path string + // Query is the URL query parameters parsed off the end of the input. + // Corresponds to env.query in [expandLink]. + Query url.Values + // IncludeLong, when true, requests that matching consider both the + // short name and the long URL. The user opts into this mode by + // prefixing their input with "?". + IncludeLong bool + // Filters holds structured filter tokens (e.g. "owner:a@b.com") that + // were stripped from the front of the input. + Filters searchFilters +} + +// FilterOnly reports whether the input carries a filter but no free text to +// fuzzy-match, i.e. a request to list every link matching the filter. +func (in searchInput) FilterOnly() bool { + return in.ShortQuery == "" && !in.Filters.Empty() +} + +// parseSearchInput parses a raw search query string. Leading "key:value" +// filter tokens are stripped first, then the "?" include-long prefix, then any +// "?k=v" query suffix and "/path" remainder. +func parseSearchInput(raw string) searchInput { + var out searchInput + s := strings.TrimLeft(raw, " \t") + + // Consume leading filter tokens. Only "owner:" is recognized as of + // 2026-10-06; unknown "key:value" tokens are left in place to be + // fuzzy-matched like ordinary text. + for { + rest, ok := strings.CutPrefix(s, "owner:") + if !ok { + break + } + val, tail, _ := strings.Cut(rest, " ") + out.Filters.Owner = val + s = strings.TrimLeft(tail, " \t") + } + + if strings.HasPrefix(s, "?") { + out.IncludeLong = true + s = s[1:] + } + // Split off any "?k=v..." query string suffix. The first '?' wins; + // that's fine because legitimate short names cannot contain '?' (see + // reShortName). + if i := strings.IndexByte(s, '?'); i >= 0 { + if q, err := url.ParseQuery(s[i+1:]); err == nil { + out.Query = q + } + s = s[:i] + } + // Split once on '/'. Left side is the short to match against; right + // side becomes Path. + if i := strings.IndexByte(s, '/'); i >= 0 { + out.ShortQuery = s[:i] + out.Path = s[i+1:] + } else { + out.ShortQuery = s + } + return out +} + +// SearchResult is a single autocomplete result, carrying enough +// information to render both the textual listbox option and the +// link destination a click would follow. +type SearchResult struct { + // Short is the link short name. Used for keyboard navigation (Enter + // on this option navigates to go/{Short}{/Path}{?Query}). + Short string `json:"short"` + // Long is the raw Link.Long field (may be a text/template). + Long string `json:"long"` + // Rendered is Long expanded through [expandLink] with the current + // user, Path, and Query, matching what a real click would resolve + // to. On expansion error, Rendered is set to Long unchanged. + Rendered string `json:"rendered"` + // RenderError is a short, human-readable note if Rendered had to + // fall back due to an expansion error. Empty on success. + RenderError string `json:"renderError,omitempty"` + // Owner is the link's owner. + Owner string `json:"owner,omitempty"` + // LastEdit is when the link was last edited. Used by the filter-only + // table listing; omitted from JSON since the autocomplete doesn't use it. + LastEdit time.Time `json:"-"` + // NumClicks is the link's current click count, read from the live + // in-memory counter (the same source the home page uses). + NumClicks int `json:"numClicks"` + // ShortMatchedIndexes are rune positions within Short that matched + // a character of the query, or empty if no short match occurred. + ShortMatchedIndexes []int `json:"shortMatchedIndexes,omitempty"` + // LongMatchedIndexes are rune positions within Long that matched a + // character of the query; populated only when the query was in + // includeLong mode and the long URL matched. Note these are + // positions within the raw Long, not Rendered. + LongMatchedIndexes []int `json:"longMatchedIndexes,omitempty"` + // ShortEscaped is Short pre-escaped for use in a URL path. Exposed + // so clients can build target URLs without re-encoding logic. + ShortEscaped string `json:"shortEscaped"` + // Target is the full path (not absolute URL) a click on this + // result should navigate to, preserving the user's Path and Query. + // Callers can just set window.location to this value. + Target string `json:"target"` + // DetailTarget is the path to the link-details page for this + // result (/.detail/{short}). Clients use this for Alt+Enter / + // Alt+click to jump into the detail/edit page instead of + // following the link. + DetailTarget string `json:"detailTarget"` +} + +// searchLimitHTML is the cap on results returned to the server-rendered +// /.search HTML page. +const searchLimitHTML = 100 + +// runSearch performs a search query end-to-end: parse the input, fuzzy +// match against the in-memory link index, expand each result's Long through +// the same template machinery the redirect path uses, and return up to +// `limit` [SearchResult]s ranked best-match first. +// +// The returned limitHit flag is true if the matcher would have produced +// additional results beyond `limit`; callers can use this to surface a +// "more results" indicator. +// +// The `cu` argument is used only for template expansion (e.g. a Long that +// references {{.User}}). Callers that don't have a resolved user should +// pass an empty string; templates that depend on .User will fail to expand +// and the raw Long will be shown. +func (s *Server) runSearch(cu string, raw string, limit int) (parsed searchInput, results []SearchResult, limitHit bool) { + in := parseSearchInput(raw) + // Empty input with no filter has nothing to match; a filter alone is a + // request to list every matching link. + if in.ShortQuery == "" && in.Filters.Empty() { + return in, nil, false + } + // Over-fetch by 1 so we can tell whether the result set was truncated + // without issuing a second query or a count. + matches := s.db.SearchShort(in.ShortQuery, in.IncludeLong, in.Filters, limit+1) + if len(matches) > limit { + matches = matches[:limit] + limitHit = true + } + results = make([]SearchResult, 0, len(matches)) + now := time.Now().UTC() + for _, m := range matches { + r := SearchResult{ + Short: m.Link.Short, + Long: m.Link.Long, + Owner: m.Link.Owner, + LastEdit: m.Link.LastEdit, + ShortMatchedIndexes: m.ShortMatchedIndexes, + LongMatchedIndexes: m.LongMatchedIndexes, + ShortEscaped: url.PathEscape(m.Link.Short), + DetailTarget: "/.detail/" + url.PathEscape(m.Link.Short), + } + // Expand the long URL using the same env that serveGo would use. + env := expandEnv{Now: now, Path: in.Path, user: cu, query: in.Query} + u, err := expandLink(m.Link.Long, env) + if err != nil { + r.Rendered = m.Link.Long + r.RenderError = err.Error() + } else { + r.Rendered = u.String() + } + // Build the target URL a click should follow. We intentionally + // route through golink's own resolver (rather than the + // rendered external URL) so click stats, template variables + // that depend on request context, and edit redirects still + // work the same way they would from any other link. + var tgt strings.Builder + tgt.WriteByte('/') + tgt.WriteString(r.ShortEscaped) + if in.Path != "" { + tgt.WriteByte('/') + tgt.WriteString(in.Path) + } + if len(in.Query) > 0 { + tgt.WriteByte('?') + tgt.WriteString(in.Query.Encode()) + } + r.Target = tgt.String() + results = append(results, r) + } + s.annotateClicks(results) + return in, results, limitHit +} + +// searchPageData is the data passed to searchTmpl for the server-rendered +// page, shared by /.search and /.all. +type searchPageData struct { + Query string // the raw, untrimmed user input + Parsed searchInput // parsed form, for showing "search mode" in the UI + Results []SearchResult // best-match-first + Limit int // max results this page was willing to render + LimitHit bool // true if more results existed but were truncated + All bool // true on /.all: list everything in the table view +} + +// TableView reports whether the results should render as the Owner/Last-Edited +// table rather than the ranked fuzzy-search list. It covers the /.all listing +// and filter-only searches (e.g. "owner:a@b.com"). +func (d searchPageData) TableView() bool { + return d.All || d.Parsed.FilterOnly() +} + +// serveSearch handles GET /.search, returning a server-rendered HTML results +// page for the parsed query. func (s *Server) serveSearch(w http.ResponseWriter, r *http.Request) { - query := r.URL.Query().Get("q") - owner, found := strings.CutPrefix(query, "owner:") - if !found { - http.Error(w, `search only supports "owner:"`, http.StatusBadRequest) - return + raw := r.URL.Query().Get("q") + // Defend against absurdly long inputs; the fuzzy matcher is linear + // in query length but pathological input could still waste CPU. + if len(raw) > 256 { + raw = raw[:256] } - links, err := s.db.GetLinksByOwner(owner) - if err != nil { + cu, err := s.currentUser(r) + if err != nil && !s.allowUnknownUsers { http.Error(w, err.Error(), http.StatusInternalServerError) return } - s.searchTmpl.Execute(w, s.searchResults(links)) + limit := searchLimitHTML + parsed, results, limitHit := s.runSearch(cu.login, raw, limit) + + if err := s.searchTmpl.Execute(w, searchPageData{ + Query: raw, + Parsed: parsed, + Results: results, + Limit: limit, + LimitHit: limitHit, + }); err != nil { + // Template execution errors after the response started streaming + // are effectively unrecoverable; log them but don't double-write. + log.Printf("searchTmpl.Execute: %v", err) + } } type expandEnv struct { diff --git a/golink_test.go b/golink_test.go index 2380102..db3ac2d 100644 --- a/golink_test.go +++ b/golink_test.go @@ -781,6 +781,36 @@ func TestHTTPSRedirectHandlerWithQuery(t *testing.T) { } } +func TestServeAll(t *testing.T) { + s := newTestServer(t) + links := []*Link{ + {Short: "alpha", Long: "http://alpha/", Owner: "foo@example.com"}, + {Short: "beta", Long: "http://beta/", Owner: "bar@example.com"}, + } + for _, link := range links { + if err := s.db.Save(link); err != nil { + t.Fatal(err) + } + } + s.stats.mu.Lock() + s.stats.clicks = ClickStats{"alpha": 42} + s.stats.mu.Unlock() + + r := httptest.NewRequest("GET", "/.all", nil) + w := httptest.NewRecorder() + s.Handler().ServeHTTP(w, r) + + if w.Code != http.StatusOK { + t.Fatalf("serveAll = %d; want %d", w.Code, http.StatusOK) + } + body := w.Body.String() + for _, s := range []string{"All links", "alpha", "beta", "2 results", "Clicks", "42"} { + if !strings.Contains(body, s) { + t.Errorf("serveAll body missing %q", s) + } + } +} + func TestServeSearch(t *testing.T) { s := newTestServer(t) links := []*Link{ @@ -806,7 +836,7 @@ func TestServeSearch(t *testing.T) { name: "search by owner with multiple links", owner: "foo@example.com", wantStatus: http.StatusOK, - wantContains: []string{"alpha", "beta", "delta", "3 total"}, + wantContains: []string{"alpha", "beta", "delta", "3 results"}, wantNotContains: []string{"gamma"}, }, { @@ -819,7 +849,7 @@ func TestServeSearch(t *testing.T) { name: "search by owner with single link", owner: "bar@example.com", wantStatus: http.StatusOK, - wantContains: []string{"gamma", "1 total"}, + wantContains: []string{"gamma", "1 result"}, wantNotContains: []string{"alpha", "beta"}, }, } @@ -850,6 +880,111 @@ func TestServeSearch(t *testing.T) { } } +func TestParseSearchInput(t *testing.T) { + tests := []struct { + name string + raw string + wantShortQuery string + wantPath string + wantQuery url.Values + wantIncludeLong bool + wantOwner string + }{ + {name: "empty", raw: ""}, + {name: "plain", raw: "foo", wantShortQuery: "foo"}, + {name: "leading whitespace", raw: " foo", wantShortQuery: "foo"}, + { + name: "short + path", + raw: "foo/bar", + wantShortQuery: "foo", + wantPath: "bar", + }, + { + name: "short + nested path", + raw: "foo/bar/baz", + wantShortQuery: "foo", + wantPath: "bar/baz", + }, + { + name: "short + query", + raw: "foo?x=1", + wantShortQuery: "foo", + wantQuery: url.Values{"x": []string{"1"}}, + }, + { + name: "short + path + query", + raw: "foo/bar/baz?x=1&y=2", + wantShortQuery: "foo", + wantPath: "bar/baz", + wantQuery: url.Values{"x": []string{"1"}, "y": []string{"2"}}, + }, + { + name: "question-mark prefix toggles includeLong", + raw: "?docs", + wantShortQuery: "docs", + wantIncludeLong: true, + }, + { + name: "question-mark prefix with whitespace", + raw: " ?docs", + wantShortQuery: "docs", + wantIncludeLong: true, + }, + { + name: "includeLong + path + query", + raw: "?foo/bar?x=1", + wantShortQuery: "foo", + wantPath: "bar", + wantQuery: url.Values{"x": []string{"1"}}, + wantIncludeLong: true, + }, + { + name: "owner filter only", + raw: "owner:a@b.com", + wantOwner: "a@b.com", + }, + { + name: "owner filter + query", + raw: "owner:a@b.com deploy", + wantShortQuery: "deploy", + wantOwner: "a@b.com", + }, + { + name: "owner filter + includeLong", + raw: "owner:a@b.com ?docs", + wantShortQuery: "docs", + wantIncludeLong: true, + wantOwner: "a@b.com", + }, + { + name: "unknown filter token is treated as text", + raw: "tag:infra", + wantShortQuery: "tag:infra", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := parseSearchInput(tt.raw) + if got.ShortQuery != tt.wantShortQuery { + t.Errorf("ShortQuery = %q, want %q", got.ShortQuery, tt.wantShortQuery) + } + if got.Path != tt.wantPath { + t.Errorf("Path = %q, want %q", got.Path, tt.wantPath) + } + if got.IncludeLong != tt.wantIncludeLong { + t.Errorf("IncludeLong = %v, want %v", got.IncludeLong, tt.wantIncludeLong) + } + if !cmp.Equal(url.Values(got.Query), tt.wantQuery) { + t.Errorf("Query = %v, want %v", got.Query, tt.wantQuery) + } + if got.Filters.Owner != tt.wantOwner { + t.Errorf("Filters.Owner = %q, want %q", got.Filters.Owner, tt.wantOwner) + } + }) + } +} + func TestSearchResults(t *testing.T) { s := newTestServer(t) s.stats.mu.Lock() @@ -884,6 +1019,95 @@ func TestSearchResults(t *testing.T) { } } +func TestServeSearchHTMLFallback(t *testing.T) { + s := newTestServer(t) + s.db.Save(&Link{Short: "hello", Long: "http://hello/"}) + s.stats.mu.Lock() + s.stats.clicks = ClickStats{"hello": 3} + s.stats.mu.Unlock() + + r := httptest.NewRequest("GET", "/.search?q=hello", nil) + // No Accept header; default to HTML fallback page. + w := httptest.NewRecorder() + s.Handler().ServeHTTP(w, r) + if w.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", w.Code, w.Body.String()) + } + body := w.Body.String() + if !strings.Contains(body, "hello") { + t.Errorf("html body missing 'hello': %s", body) + } + // The unified table shows a Clicks column with the count. + if !strings.Contains(body, "Clicks") { + t.Errorf("html body missing Clicks column; body=%s", body) + } + // Fuzzy matches are highlighted via in the shared table. + if !strings.Contains(body, "") { + t.Errorf("html body missing fuzzy highlight markup; body=%s", body) + } + // Should be a full HTML page via base.html, which includes the footer + // "From the nerds at". + if !strings.Contains(body, "From the nerds at") { + t.Errorf("html body missing base.html chrome") + } + // The page-level search form should be present, prefilled with + // the user's query, and carry autocomplete=off. + if !strings.Contains(body, `id="gl-search-page-input"`) { + t.Errorf("html body missing page-level search input") + } + if !strings.Contains(body, `value="hello"`) { + t.Errorf("html body should prefill search input with q value; body=%s", body) + } + if !strings.Contains(body, `autocomplete="off"`) { + t.Errorf("html body should have autocomplete=off on page-level input") + } +} + +// TestServeSearchHTMLQueryEscaping verifies the page-level search +// input's prefill value is HTML-escaped (prevents injection via the +// ?q= parameter). +func TestServeSearchHTMLQueryEscaping(t *testing.T) { + s := newTestServer(t) + + r := httptest.NewRequest("GET", "/.search?q="+url.QueryEscape(`">`), nil) + w := httptest.NewRecorder() + s.Handler().ServeHTTP(w, r) + body := w.Body.String() + if strings.Contains(body, "") { + t.Errorf("html body should escape query; got body containing unescaped