Skip to content
Closed
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
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,62 @@ Users just need to have Tailscale installed and connected to the tailnet.

[MagicDNS]: https://tailscale.com/kb/1081/magicdns/

## Browser OpenSearch integration

golink publishes an OpenSearch descriptor at `/.opensearch` and links to it
from the main page. Once installed in your browser, you can search golink
directly from the address bar/search UI and receive live suggestions.

### Firefox

1. Visit your golink home page (for example `http://go/`).
2. Open Firefox settings at `about:preferences#search`.
3. In the search engine list, add/select the discovered `go` engine.
4. Use it from the address bar by typing `@go <query>`.

If the engine isn't discovered automatically, add it manually under
**Additional search engines** → **Add**, filling in the *Edit Search Engine*
dialog:

| Field | Value |
| --- | --- |
| Search engine name | `Go` |
| URL with %s in place of search term | `http://go/%s` |
| Keyword (optional) | `@go` |
| POST data with %s in place of search term | *(leave empty for GET)* |
| Suggestions URL with %s in place of search term (optional) | `http://go/.opensearch/suggest?q=%s` |

Then click **Save Engine**.

Notes:
- Substitute your deployed host for `go` if you serve golink elsewhere (for
example `http://go.example.com/%s`).
- If you've previously added a manual search engine, remove it first so Firefox
prefers the OpenSearch-discovered one.

### Chrome

1. Visit your golink home page once.
2. Open `chrome://settings/searchEngines`.
3. Under site search shortcuts, find the auto-discovered golink entry.
4. Update its keyword to something short like `@go` (the URL is already correct).

Use it by typing the keyword in the address bar, pressing `Tab`, then entering
the query.

### Safari

Safari's Quick Website Search is powered by OpenSearch and lets you search
golink from the address bar after you've visited it once.

1. Enable it under **Settings** → **Search** → **Enable Quick Website Search**.
2. Visit your golink home page (for example `http://go/`) and run a search so
Safari records the site.
3. Click **Manage Websites…** next to that setting to see the list of sites
Quick Website Search is enabled for; golink appears under whatever short name
you access it by (for example `go`).
4. Search from the address bar by typing that short name followed by your query.

## Running in production

golink compiles as a single static binary (including the frontend) and can be deployed and run like any other binary.
Expand Down
271 changes: 254 additions & 17 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"fmt"
"io/fs"
"net/url"
"sort"
"strings"
"sync"
"time"

"github.com/sahilm/fuzzy"
_ "modernc.org/sqlite"
"tailscale.com/tstime"
)
Expand All @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -138,9 +201,65 @@ 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
}

// SaveAll saves multiple Links in a single transaction. Existing links with
// the same ID are not overwritten. It returns the number of rows that were
// newly inserted.
func (s *SQLiteDB) SaveAll(links []*Link) (int, error) {
s.mu.Lock()
defer s.mu.Unlock()

tx, err := s.db.BeginTx(context.TODO(), nil)
if err != nil {
return 0, err
}
stmt, err := tx.Prepare("INSERT OR IGNORE INTO Links (ID, Short, Long, Created, LastEdit, Owner) VALUES (?, ?, ?, ?, ?, ?)")
if err != nil {
tx.Rollback()
return 0, err
}
defer stmt.Close()

// 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 len(insertedLinks), err
}
rows, err := result.RowsAffected()
if err != nil {
tx.Rollback()
return len(insertedLinks), err
}
if rows == 1 {
insertedLinks = append(insertedLinks, link)
}
}
if err := tx.Commit(); err != nil {
return 0, err
}
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.
func (s *SQLiteDB) Delete(short string) error {
s.mu.Lock()
Expand All @@ -157,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
}

Expand Down Expand Up @@ -226,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 }
Loading
Loading