Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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