$ show tailscale/golink
golink is a private shortlink service (http://go/) for Tailscale tailnets. It allows users to create short aliases for URLs, supports Go text/template expansion for dynamic links, and integrates deeply with Tailscale identity and networking via the tsnet library. The architecture is a single Go binary that uses an embedded SQLite database for persistent storage and serves both a web UI (TailwindCSS + Go templates) and a JSON API. It handles user authentication through Tailscale's WhoIs mechanism, supports admin grants via ACL capabilities, and provides backup/restore via JSON Lines export. The codebase is well-structured, with clear separation between database operations (db.go), HTTP handlers and link resolution logic (golink.go), and the CLI main entry point. The repo includes comprehensive tests, Docker/Nix/Helm deployment options, and Prometheus metrics. It demonstrates high craftsmanship in areas such as idempotent snapshot restoration, template-based link expansion with path/query forwarding, and robust XSRF protection.
The LLM identified 4 files worth highlighting for reuse:
The expandLink function is a well-crafted, self-contained component that expands a link's long URL using Go text/templates. It handles both simple appending of remaining path and complex template logic with functions like PathEscape, QueryEscape, and regex matching. The function is reusable for any application needing URL template expansion with a context struct. Additionally, the HSTS middleware and redirectHandler are clean, testable patterns for HTTP server middleware.
The SQLiteDB type provides a complete, thread-safe data access layer for Links and click statistics. It uses a sync.RWMutex for concurrent access, embeds the schema via //go:embed, and returns fs.ErrNotExist for missing links. The Save, Load, Delete, and LoadStats methods follow a consistent transactional pattern. This component could be extracted and reused for any Go application needing a simple SQLite-backed key-value store with stats tracking.
The test file demonstrates high-quality table-driven tests for HTTP handlers, link expansion, and business logic. It uses httptest for mocking HTTP requests/responses, overrides the currentUser function for auth testing, and includes edge cases like trailing punctuation, query strings, and error conditions. The TestExpandLink function specifically is a comprehensive, self-contained test suite for the template expansion logic that could serve as a template for testing similar URL manipulation code.
The main.go file is a minimal, clean entry point that simply calls golink.Run(). It uses a blank import of the golink package and delegates all logic. This pattern of thin main packages is reusable for any Go service that wants to keep CLI logic separate from library code and is a best practice for Go projects.