httpapi

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2020 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package httpapi provides common encoding and middleware for an HTTP API.

Index

Constants

View Source
const HHMM = "15:04"

HHMM formats a timestamp as HH:MM Reference: https://yourbasic.org/golang/format-parse-string-time-date-example/

View Source
const HHMMSS = "15:04:05"

HHMMSS formats a timestamp as HH:MM:SS Reference: https://yourbasic.org/golang/format-parse-string-time-date-example/

Variables

This section is empty.

Functions

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, err error)

ErrorResponse writes an error response. Domain errors are returned to the client. Any other errors, will resolve to 500 error response.

func GetIP added in v0.4.0

func GetIP(r *http.Request) string

GetIP Retrieves the client IP address

func GetRefreshToken added in v0.2.0

func GetRefreshToken(r *http.Request) string

GetRefreshToken retrieves a Refresh Token from context.

func GetToken

func GetToken(r *http.Request) *auth.Token

GetToken retrieves a Token from context.

func GetUserID

func GetUserID(r *http.Request) string

GetUserID retrieves a User ID from context.

func JSONResponse

func JSONResponse(w http.ResponseWriter, v interface{}, statusCode int)

JSONResponse writes a response body. If a struct is provided and we are unable to marshal it, we return an internal error.

func ToHandlerFunc

func ToHandlerFunc(jsonHandler JSONAPIHandler, successCode int) http.HandlerFunc

ToHandlerFunc adapts a JSONAPIHandler into net/http's HandlerFunc.

Types

type JSONAPIHandler

type JSONAPIHandler func(w http.ResponseWriter, r *http.Request) (interface{}, error)

JSONAPIHandler is an HTTP handler for a JSON API.

func AuthMiddleware

func AuthMiddleware(jsonHandler JSONAPIHandler, tokenSvc auth.TokenService, state auth.TokenState) JSONAPIHandler

AuthMiddleware validates an Authorization header if available.

func ErrorLoggingMiddleware

func ErrorLoggingMiddleware(jsonHandler JSONAPIHandler, log log.Logger) JSONAPIHandler

ErrorLoggingMiddleware logs any errors that are returned before being parsed to an HTTP response.

func RateLimitMiddleware added in v0.3.0

func RateLimitMiddleware(jsonHandler JSONAPIHandler, lmt Limiter) JSONAPIHandler

RateLimitMiddleware rate limits HTTP requests.

func RefreshTokenMiddleware added in v0.2.0

func RefreshTokenMiddleware(jsonHandler JSONAPIHandler) JSONAPIHandler

RefreshTokenMiddleware sets a refresh token in context.

type Limiter added in v0.4.0

type Limiter interface {
	// RateLimit applies basic rate limiting to an HTTP request.
	RateLimit(r *http.Request) error
}

Limiter provides rate limiting tooling

type LimiterFactory added in v0.4.0

type LimiterFactory interface {
	// NewLimiter returns a new Limiter.
	NewLimiter(prefix string, rate Rate, max int64) Limiter
}

LimiterFactory creates new Limiters

func NewRateLimiter added in v0.4.0

func NewRateLimiter(db rediser) LimiterFactory

NewRateLimiter returns a new Limiter.

type MockLimiter added in v0.4.0

type MockLimiter struct{}

MockLimiter is a stub for Limiter interface.

func (*MockLimiter) RateLimit added in v0.4.0

func (m *MockLimiter) RateLimit(r *http.Request) error

RateLimit mock.

type MockLimiterFactory added in v0.4.0

type MockLimiterFactory struct{}

MockLimiterFactory is a stub for LimiterFactory interface.

func (*MockLimiterFactory) NewLimiter added in v0.4.0

func (m *MockLimiterFactory) NewLimiter(pefix string, rate Rate, max int64) Limiter

NewLimiter mock.

type Rate added in v0.4.0

type Rate string

Rate is the rate of allowed requests. We support r/min and r/second.

const (
	// PerSecond allows us to accept x requests per second
	PerSecond Rate = "per_second"
	// PerMinute allows us to accept x requests per minute
	PerMinute = "per_minute"
)

Jump to

Keyboard shortcuts

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