api

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 12 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidOffset is returned when the requested offset is invalid.
	ErrInvalidOffset = errors.New("offset must be non-negative")

	// ErrInvalidLimit is returned when the requested limit is invalid.
	ErrInvalidLimit = fmt.Errorf("limit must between 1 and %d", maxLimit)

	// ErrInvalidSortPair is returned when the requested sort parameters are
	// invalid.
	ErrInvalidSortPair = fmt.Errorf("'sortby' must be a valid field and 'desc' must be a boolean")

	// ErrMissingSortPair is returned when only one of the sort parameters is
	// provided.
	ErrMissingSortPair = errors.New("must provide both 'sortby' and 'desc' parameters")
)

Functions

func ClientIP

func ClientIP(r *http.Request) string

ClientIP extracts the client IP from the request. It checks X-Forwarded-For first (for reverse proxy deployments) and falls back to RemoteAddr. IPv6 addresses are normalized to their /64 subnet to prevent attackers from rotating addresses within a subnet.

func ParseOffsetLimit

func ParseOffsetLimit(jc jape.Context) (offset int, limit int, ok bool)

ParseOffsetLimit parses the 'offset' and 'limit' query parameters from the request context. It returns the offset and limit values, and a boolean indicating whether the parsing was successful. If the parameters are not present or invalid, it returns false and writes an appropriate error to the response body.

Types

type IPRateLimiter

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

An IPRateLimiter is a per-key rate limiter using token buckets. Each unique key gets its own bucket that is automatically pruned after the TTL expires.

func NewIPRateLimiter

func NewIPRateLimiter(every time.Duration, burst int, ttl time.Duration) *IPRateLimiter

NewIPRateLimiter creates a new per-key rate limiter. every is the minimum time between requests in steady state, burst is the maximum number of requests allowed at once, and ttl is how long a bucket is retained after creation before being pruned.

func (*IPRateLimiter) Allow

func (rl *IPRateLimiter) Allow(key string) bool

Allow returns true if the request for the given key is within the rate limit.

type SortOption

type SortOption struct {
	Field      string
	Descending bool
}

SortOption represents a single sorting configuration parsed from request parameters.

func ParseSortOptions

func ParseSortOptions(jc jape.Context) (sorts []SortOption, ok bool)

ParseSortOptions parses 'sortby' and 'desc' query parameters from the request context. It returns the parsed sort options and a boolean indicating whether parsing succeeded. If invalid parameters are provided an error is written to the response and false is returned.

type URLQueryParameterOption

type URLQueryParameterOption func(url.Values)

URLQueryParameterOption is an option to configure the query string parameters.

func WithConnectKey

func WithConnectKey(connectKey string) URLQueryParameterOption

WithConnectKey sets the 'connectkey' parameter.

func WithCountry

func WithCountry(countryCode string) URLQueryParameterOption

WithCountry sets the 'country' parameter in Hosts

func WithLimit

func WithLimit(limit int) URLQueryParameterOption

WithLimit sets the 'limit' parameter.

func WithOffset

func WithOffset(offset int) URLQueryParameterOption

WithOffset sets the 'offset' parameter.

func WithProtocol

func WithProtocol(protocol chain.Protocol) URLQueryParameterOption

WithProtocol sets the 'protocol' parameter in Hosts

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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