rest

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: CC-BY-SA-4.0 Imports: 7 Imported by: 0

Documentation

Overview

Package rest provides link services with a REST API over HTTP.

Package rest provides link management services over HTTP.

Main components

  • Shorten - Shortens a URL.
  • Resolve - Resolves a shortened URL.
  • Health - Checks the health of the service.

Example handler usage

mux := http.NewServeMux()
mux.Handle("POST /shorten", Shorten(...))
mux.Handle("GET /r/{key}", Resolve(...))
mux.HandleFunc("GET /health", Health)

Shorten a URL:

$ curl localhost:8080/shorten -d '{"url":"https://x.com/inancgumus"}'

Resolve a shortened URL:

$ curl localhost:8080/r/639508a7

Health check:

$ curl localhost:8080/health

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Health

func Health(w http.ResponseWriter, r *http.Request)

Health serves the health check requests.

func Resolve

func Resolve(lg *slog.Logger, links Resolver) http.Handler

Resolve returns an HTTP handler that resolves shortened link URLs. It extracts a {key} from http.Request using http.Request.PathValue.

func Shorten

func Shorten(lg *slog.Logger, links Shortener) http.Handler

Shorten returns an http.Handler that shortens URLs.

Types

type Resolver

type Resolver interface {
	// Resolve retrieves a link by its key.
	Resolve(context.Context, link.Key) (link.Link, error)
}

Resolver is a link resolver service that resolves shortened URLs.

type Shortener

type Shortener interface {
	// Shorten shortens a link and returns its key.
	Shorten(context.Context, link.Link) (link.Key, error)
}

Shortener is a link shortener service that shortens URLs.

Jump to

Keyboard shortcuts

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