shortlinks

package
v1.5.1-0...-475df33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package shortlinks implements the embedded link-shortener service.

Index

Constants

View Source
const (
	MinCodeLength = 4
	MaxCodeLength = 10
)

Variables

View Source
var CodeRegex = regexp.MustCompile(fmt.Sprintf(`^[0-9A-Za-z]{%d,%d}$`, MinCodeLength, MaxCodeLength))

CodeRegex mirrors shortLinks.ts's CODE_REGEX.

Functions

func Middleware

func Middleware(cfg *config.Loaded, store *Store) func(http.Handler) http.Handler

Middleware mirrors ShortLinks.router: a chainable http middleware (Express's `app.use`) that intercepts requests under the configured short-link path prefix and falls through to next for everything else.

Types

type Cleaner

type Cleaner struct {
	// contains filtered or unexported fields
}

Cleaner mirrors ShortLinks.startCleaner/cleanExpiredLinks: a background loop that periodically deletes expired short links, wrapped in a struct (rather than a bare goroutine) so tests can Stop() it deterministically.

func NewCleaner

func NewCleaner(store *Store) *Cleaner

NewCleaner constructs a Cleaner. It mirrors startCleaner's own guard: if the configured expirationTime is disabled (< 1), Start is a no-op.

func (*Cleaner) Start

func (c *Cleaner) Start(cfg *config.Loaded)

Start mirrors ShortLinks.startCleaner.

func (*Cleaner) Stop

func (c *Cleaner) Stop()

Stop halts the background loop. Not present in the Node original (which runs for the lifetime of the process) but needed so Go tests don't leak timers past the test that started one.

type CreateResult

type CreateResult struct {
	Code     string
	ShortURL string
}

CreateResult mirrors the `{code, shortUrl}` shape ShortLinks.create resolves to.

type Entry

type Entry struct {
	Code      string `json:"code"`
	URL       string `json:"url"`
	ShortPath string `json:"shortPath,omitempty"`
	CreatedAt int64  `json:"createdAt"`
	ExpiresAt *int64 `json:"expiresAt"`
}

Entry mirrors client/js/types/shortlink.ts's ShortLinkEntry.

type ResolveResult

type ResolveResult struct {
	URL  string
	Gone bool
}

ResolveResult mirrors the `{url} | {gone: true} | null` union ShortLinks.resolve returns. A nil *ResolveResult (with nil error) means the code doesn't exist at all.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store mirrors shortLinks.ts's module-level `database` singleton plus the static ShortLinks methods that operate on it, wrapped in a struct so tests can point multiple independent instances at their own temp files instead of sharing Node's single process-wide singleton.

func Open

func Open(path string) (*Store, error)

Open mirrors getDatabase(): opens (creating if necessary) the sqlite file at path, sets its pragmas, and ensures the schema exists.

func (s *Store) CleanExpiredLinks() (int64, error)

CleanExpiredLinks mirrors ShortLinks.cleanExpiredLinks, returning the number of rows deleted.

func (*Store) Close

func (s *Store) Close() error

Close closes the underlying database.

func (*Store) Create

func (s *Store) Create(username, url string, cfg config.ShortLink) (*CreateResult, error)

Create mirrors ShortLinks.create. cfg is the live shortLink config section (codeLength/expirationTime/path/baseUrl are all read at call time, not cached, matching Config.values being read live in the TS version). A nil result with a nil error means the url was rejected or code generation exhausted its attempts - both are "no short link created", not application errors, mirroring create()'s `return null`.

func (*Store) DB

func (s *Store) DB() *sql.DB

DB exposes the underlying *sql.DB for tests that want to poke at rows directly, mirroring the TS test suite's direct use of getDatabase().

func (s *Store) DeleteUserLink(username, code string) error

DeleteUserLink mirrors ShortLinks.deleteUserLink.

func (s *Store) GetUserLinks(username string, pathPrefix string) ([]Entry, error)

GetUserLinks mirrors ShortLinks.getUserLinks.

func (*Store) Resolve

func (s *Store) Resolve(code string) (*ResolveResult, error)

Resolve mirrors ShortLinks.resolve, including the delete-on-expiry side effect.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL