Documentation
¶
Overview ¶
Package shortlinks implements the embedded link-shortener service.
Index ¶
- Constants
- Variables
- func Middleware(cfg *config.Loaded, store *Store) func(http.Handler) http.Handler
- type Cleaner
- type CreateResult
- type Entry
- type ResolveResult
- type Store
- func (s *Store) CleanExpiredLinks() (int64, error)
- func (s *Store) Close() error
- func (s *Store) Create(username, url string, cfg config.ShortLink) (*CreateResult, error)
- func (s *Store) DB() *sql.DB
- func (s *Store) DeleteUserLink(username, code string) error
- func (s *Store) GetUserLinks(username string, pathPrefix string) ([]Entry, error)
- func (s *Store) Resolve(code string) (*ResolveResult, error)
Constants ¶
const ( MinCodeLength = 4 MaxCodeLength = 10 )
Variables ¶
var CodeRegex = regexp.MustCompile(fmt.Sprintf(`^[0-9A-Za-z]{%d,%d}$`, MinCodeLength, MaxCodeLength))
CodeRegex mirrors shortLinks.ts's CODE_REGEX.
Functions ¶
func Middleware ¶
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 ¶
NewCleaner constructs a Cleaner. It mirrors startCleaner's own guard: if the configured expirationTime is disabled (< 1), Start is a no-op.
type CreateResult ¶
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 ¶
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 ¶
Open mirrors getDatabase(): opens (creating if necessary) the sqlite file at path, sets its pragmas, and ensures the schema exists.
func (*Store) CleanExpiredLinks ¶
CleanExpiredLinks mirrors ShortLinks.cleanExpiredLinks, returning the number of rows deleted.
func (*Store) Create ¶
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 ¶
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 (*Store) DeleteUserLink ¶
DeleteUserLink mirrors ShortLinks.deleteUserLink.
func (*Store) GetUserLinks ¶
GetUserLinks mirrors ShortLinks.getUserLinks.