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 ¶
- Constants
- func AddCORSHandler(r *mux.Router)
- func GetRequestID(r *http.Request) string
- func GetSkipAndCount[T *http.Request | string](t T) (skip int, count int, exists bool, err error)
- func GetUserID(r *http.Request) string
- func InternalError(w http.ResponseWriter, err error) string
- func LimitedSkipCount[T *http.Request | string](t T, skipLimit, countLimit int) (skip int, count int, exists bool, err error)
- func OriginAllowedForCORS(origin string) bool
- func Problem(w http.ResponseWriter, err error)
- func ResetCORSAllowlistForTest()
- func SetAccessControlAllowHeaders(w http.ResponseWriter, origin string)
- func SetCORSAllowedOrigins(origins []string)
- type Direction
- type OrderBy
- type ResponseWriter
Constants ¶
const CORSAllowedOriginsEnv = "MOOV_CORS_ALLOW_ORIGINS"
CORSAllowedOriginsEnv is a comma-separated list of exact Origins permitted for credentialed CORS. Example: "https://moov.io,https://dashboard.moov.io".
Services that use AddCORSHandler, SetAccessControlAllowHeaders, or Wrap honor this allowlist automatically after upgrading moov-io/base — set the env var in deploy. Local development origins (http://localhost[:port], http://127.0.0.1[:port]) remain allowed.
Variables ¶
This section is empty.
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 GetSkipAndCount ¶ added in v0.11.1
GetSkipAndCount returns the skip and count pagination values from the query parameters - skip is the number of records to pass over before starting a search (max math.MaxInt32) - count is the number of records to retrieve in the search (max 10,000) - exists indicates if skip or count was passed into the request URL
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 LimitedSkipCount ¶ added in v0.39.0
func LimitedSkipCount[T *http.Request | string](t T, skipLimit, countLimit int) (skip int, count int, exists bool, err error)
LimitedSkipCount returns the skip and count pagination values from the request's query parameters See GetSkipAndCount for descriptions of each parameter
func OriginAllowedForCORS ¶ added in v0.63.0
OriginAllowedForCORS reports whether origin may receive credentialed CORS headers. Empty origins are rejected. Localhost / 127.0.0.1 HTTP origins are always allowed for local development; all other origins must appear in the allowlist.
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 ResetCORSAllowlistForTest ¶ added in v0.63.0
func ResetCORSAllowlistForTest()
ResetCORSAllowlistForTest clears the cached allowlist so the next use reloads from MOOV_CORS_ALLOW_ORIGINS. Intended for tests only.
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. Used by AddCORSHandler and Wrap; services can call it directly (e.g. ACH go-kit ServerAfter) and share the same allowlist.
func SetCORSAllowedOrigins ¶ added in v0.63.0
func SetCORSAllowedOrigins(origins []string)
SetCORSAllowedOrigins replaces the in-process CORS allowlist with the given exact Origins. Pass nil or empty to clear. Useful for tests and for services that load allowed origins from config rather than (or in addition to) the env var.
Safe for concurrent use with SetAccessControlAllowHeaders / Wrap.
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 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