Documentation
¶
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 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 IsShutdown ¶
IsShutdown checks to see if the shutdown error 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 Respond ¶
Respond converts a Go value to JSON and sends it to the client. If code is StatusNoContent, v is expected to be nil.
func RespondError ¶
RespondError sends an error reponse back to the client.
Types ¶
type App ¶
type App struct { *httptreemux.TreeMux // contains filtered or unexported fields }
App is the entrypoint into our application and what configures our context object for each of our http handlers. Feel free to add any configuration data/logic on this App struct
func (*App) Handle ¶
func (a *App) Handle(verb, path string, handler Handler, mw ...Middleware)
Handle is our mechanism for mounting Handlers for a given HTTP verb and path pair, this makes for really easy, convenient routing.
func (*App) ServeHTTP ¶
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface. It overrides the ServeHTTP of the embedded TreeMux by using the ochttp.Handler instead. That Handler wraps the TreeMux handler so the routes are served.
func (*App) SignalShutdown ¶
func (a *App) SignalShutdown()
SignalShutdown is used to gracefully shutdown the app when an integrity issue is identified.
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 Handler ¶
type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request, params map[string]string) error
A Handler is a type that handles an http request within our own little mini framework.
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.