Documentation ¶
Overview ¶
Package http implements a core suite of HTTP functions for use inside Moov. These packages are designed to be used in production to provide insight without an excessive performance tradeoff.
This package implements several opininated response functions (See Problem, InternalError) and stateless CORS handling under our load balancing setup. They may not work for you.
This package also implements a wrapper around http.ResponseWriter to log X-Request-ID, timing and the resulting status code.
Index ¶
- Variables
- func AddCORSHandler(r *mux.Router)
- func GetRequestID(r *http.Request) string
- func GetUserID(r *http.Request) string
- func InternalError(w http.ResponseWriter, err error) string
- func Problem(w http.ResponseWriter, err error)
- func SetAccessControlAllowHeaders(w http.ResponseWriter, origin string)
- type ResponseWriter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoUserID is returned when no x-user-id header was found ErrNoUserID = errors.New("no X-User-Id header provided") )
Functions ¶
func AddCORSHandler ¶
AddCORSHandler captures Corss Origin Resource Sharing (CORS) requests by looking at all OPTIONS requests for the Origin header, parsing that and responding back with the other Access-Control-Allow-* headers.
Docs: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
func GetRequestID ¶ added in v0.10.0
GetRequestID returns the Moov header value for request IDs
func InternalError ¶
func InternalError(w http.ResponseWriter, err error) string
InternalError writes err to w while also setting the HTTP status code, content-type and marshaling err as the response body.
Returned is the calling file and line number: server.go:33
func Problem ¶
func Problem(w http.ResponseWriter, err error)
Problem writes err to w while also setting the HTTP status code, content-type and marshaling err as the response body.
func SetAccessControlAllowHeaders ¶
func SetAccessControlAllowHeaders(w http.ResponseWriter, origin string)
SetAccessControlAllowHeaders writes Access-Control-Allow-* headers to a response to allow for further CORS-allowed requests.
Types ¶
type ResponseWriter ¶
type ResponseWriter struct { http.ResponseWriter // contains filtered or unexported fields }
ResponseWriter implements Go's standard library http.ResponseWriter to complete HTTP requests
func EnsureHeaders ¶
func EnsureHeaders(logger log.Logger, m metrics.Histogram, rec idempotent.Recorder, w http.ResponseWriter, r *http.Request) (*ResponseWriter, error)
EnsureHeaders wraps the http.ResponseWriter but also checks Moov specific headers.
X-User-Id is required, and requests without one will be completed with a 403 forbidden. No lookup is done to ensure the value exists and is valid for a Moov user.
X-Request-Id is optional, but if used we will emit a log line with that request fulfillment timing and the status code.
X-Idempotency-Key is optional, but recommended to ensure requests only execute once. Clients are assumed to resend requests many times with the same key. We just need to reply back "already done".
func Wrap ¶
func Wrap(logger log.Logger, m metrics.Histogram, w http.ResponseWriter, r *http.Request) *ResponseWriter
Wrap returns a ResponseWriter usable by applications. No parts of the Request are inspected or ResponseWriter modified.
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(code int)
WriteHeader sends an HTTP response header with the provided status code, records response duration, and optionally records the HTTP metadata in a go-kit log.Logger