Documentation ¶
Overview ¶
Package web contains gluecode for abstracting observability, middlewares and error handling around http Handlers
Index ¶
- Constants
- func Decode(r *http.Request, val interface{}) error
- func IsShutdown(err error) bool
- func NewRequestError(err error, status int) error
- func NewShutdownError(message string) error
- func Param(r *http.Request, key string) string
- func Respond(ctx context.Context, w http.ResponseWriter, data interface{}, statusCode int) error
- func RespondError(ctx context.Context, w http.ResponseWriter, err error) error
- type App
- type Error
- type ErrorResponse
- type FieldError
- type Handler
- type Middleware
- type Values
Constants ¶
const KeyValues ctxKey = 1
KeyValues is how request metadata (type Values) are stored/retrieved.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode gets the JSON value from the request body and decode it. If the provided value is a struct then it is checked for validation tags.
func IsShutdown ¶
IsShutdown checks to see if the shutdown error type is contained in the specified error value.
func NewRequestError ¶
NewRequestError wraps a provided error with an HTTP status code. This function should be used when handlers encounter expected errors.
func NewShutdownError ¶
NewShutdownError returns an error that causes the framework to signal a graceful shutdown.
func RespondError ¶
RespondError sends an error response back to clients.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
Type App is the entrypoint into the web application, it configures context for http handlers and hooks up os.Signal from application inner layers. This can be extended for further behaviors.
func NewApp ¶
func NewApp(shutdown chan os.Signal, mw ...Middleware) *App
Factory method for creating concrete App that handles http routes handling
func (*App) Handle ¶
func (a *App) Handle(method string, path string, handler Handler, mw ...Middleware)
Handle encapsulates concrete http.HandleFunc calls to abstract requests observability and error handling
func (*App) ServeHTTP ¶
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the entry point for all http traffic and allows the opentelemetry mux to run first to handle tracing. The opentelemetry mux then calls the application mux to handle application traffic.
func (*App) SignalShutdown ¶
func (a *App) SignalShutdown()
SignalShutdown is for gracefully shutdown the application process hooking up OS signals
type Error ¶
type Error struct { Err error Status int Fields []FieldError }
Error is used to pass an error during the request through the application with specific context.
type ErrorResponse ¶
type ErrorResponse struct { Error string `json:"error"` Fields []FieldError `json:"fields,omitempty"` }
ErrorResponse is for API responses.
type FieldError ¶
FieldError is used to indicate an error with a specific request field.
type Handler ¶
Type Handler is an adapter to allow the use of custom method signature as native http.HandlerFunc
type Middleware ¶
Middleware function helps to run some code before and/or after another Handler.