httpapi

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const ThrottleEveryFiveMin = float64(.003)

ThrottleEveryFiveMin limits a user requests to roughly every five minutes.

View Source
const ThrottleEveryOneSec = float64(1)

ThrottleEveryOneSec limits a user requests to roughly every second.

View Source
const ThrottleEveryTenSec = float64(.1)

ThrottleEveryTenSec limits a user requests to roughly every ten seconds.

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 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, source string, 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.Limiter) JSONAPIHandler

RateLimitMiddleware is an adapter to use tollbooth's ratelimiting library with our HTTP middleware.

func RefreshTokenMiddleware added in v0.2.0

func RefreshTokenMiddleware(jsonHandler JSONAPIHandler) JSONAPIHandler

RefreshTokenMiddleware sets a refresh token in context.

Jump to

Keyboard shortcuts

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