Documentation ¶
Index ¶
- Constants
- func ContextRequestID(ctx context.Context) (reqID string)
- func ErrorHandler(service *goa.Service, verbose bool) goa.Middleware
- func LogRequest(verbose bool) goa.Middleware
- func LogResponse() goa.Middleware
- func Recover() goa.Middleware
- func RequestID() goa.Middleware
- func RequestIDWithHeader(requestIDHeader string) goa.Middleware
- func RequireHeader(service *goa.Service, pathPattern *regexp.Regexp, requiredHeaderName string, ...) goa.Middleware
- func Timeout(timeout time.Duration) goa.Middleware
Constants ¶
const RequestIDHeader = "X-Request-Id"
RequestIDHeader is the name of the header used to transmit the request ID.
Variables ¶
This section is empty.
Functions ¶
func ContextRequestID ¶
ContextRequestID extracts the Request ID from the context.
func ErrorHandler ¶
func ErrorHandler(service *goa.Service, verbose bool) goa.Middleware
ErrorHandler turns a Go error into an HTTP response. It should be placed in the middleware chain below the logger middleware so the logger properly logs the HTTP response. ErrorHandler understands instances of goa.Error and returns the status and response body embodied in them, it turns other Go error types into a 500 internal error response. If verbose is false the details of internal errors is not included in HTTP responses.
func LogRequest ¶
func LogRequest(verbose bool) goa.Middleware
LogRequest creates a request logger middleware. This middleware is aware of the RequestID middleware and if registered after it leverages the request ID for logging. If verbose is true then the middlware logs the request and response bodies.
func LogResponse ¶
func LogResponse() goa.Middleware
LogResponse creates a response logger middleware. Only Logs the raw response data without accumulating any statistics.
func Recover ¶
func Recover() goa.Middleware
Recover is a middleware that recovers panics and maps them to errors.
func RequestID ¶
func RequestID() goa.Middleware
RequestID is a middleware that injects a request ID into the context of each request. Retrieve it using ctx.Value(ReqIDKey). If the incoming request has a RequestIDHeader header then that value is used else a random value is generated.
func RequestIDWithHeader ¶
func RequestIDWithHeader(requestIDHeader string) goa.Middleware
RequestIDWithHeader behaves like the middleware RequestID, but it takes the request id header as the (first) argument.
func RequireHeader ¶
func RequireHeader( service *goa.Service, pathPattern *regexp.Regexp, requiredHeaderName string, requiredHeaderValue *regexp.Regexp, failureStatus int) goa.Middleware
RequireHeader requires a request header to match a value pattern. If the header is missing or does not match then the failureStatus is the response (e.g. http.StatusUnauthorized). If pathPattern is nil then any path is included. If requiredHeaderValue is nil then any value is accepted so long as the header is non-empty.
func Timeout ¶
func Timeout(timeout time.Duration) goa.Middleware
Timeout sets a global timeout for all controller actions. The timeout notification is made through the context, it is the responsability of the request handler to handle it. For example:
func (ctrl *Controller) DoLongRunningAction(ctx *DoLongRunningActionContext) error { action := NewLongRunning() // setup long running action c := make(chan error, 1) // create return channel go func() { c <- action.Run() } // Launch long running action goroutine select { case <- ctx.Done(): // timeout triggered action.Cancel() // cancel long running action <-c // wait for Run to return. return ctx.Err() // retrieve cancel reason case err := <-c: // action finished on time return err // forward its return value } }
Package golang.org/x/net/context/ctxhttp contains an implementation of an HTTP client which is context-aware:
func (ctrl *Controller) HttpAction(ctx *HttpActionContext) error { req, err := http.NewRequest("GET", "http://iamaslowservice.com", nil) // ... resp, err := ctxhttp.Do(ctx, nil, req) // returns if timeout triggers // ... }
Controller actions can check if a timeout is set by calling the context Deadline method.
Types ¶
This section is empty.