Documentation ¶
Overview ¶
Package web provides a minimal framework for common HTTP related tasks.
Index ¶
- Constants
- func Decode(r *http.Request, val interface{}) error
- func NewRequestError(err error, status int) error
- 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 values or stored/retrieved.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode reads the body of an HTTP request looking for a JSON document. The body is decoded into the provided value.
If the provided value is a struct then it is checked for validation tags.
func NewRequestError ¶
NewRequestError wraps a provided error with an HTTP status code. This function should be used when handlers encounter expected errors.
func RespondError ¶
RespondError sends an error reponse back to the client.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the entrypoint into our application and what controls the context of each request. Feel free to add any configuration data/logic on this type.
func NewApp ¶
func NewApp(log *log.Logger, mw ...Middleware) *App
NewApp constructs an App to handle a set of routes. Any Middleware provided will be ran for every request.
func (*App) Handle ¶
func (a *App) Handle(method, url string, h Handler, mw ...Middleware)
Handle associates a handler function with an HTTP Method and URL pattern.
It converts our custom handler type to the std lib Handler type. It captures errors from the handler and serves them to the client in a uniform way.
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 web specific context.
type ErrorResponse ¶
type ErrorResponse struct { Error string `json:"error"` Fields []FieldError `json:"fields,omitempty"` }
ErrorResponse is the form used for API responses from failures in the API.
type FieldError ¶
FieldError is used to indicate an error with a specific request field.
type Middleware ¶
Middleware is a function designed to run some code before and/or after another Handler. It is designed to remove boilerplate or other concerns not direct to any given Handler.