router

package
v0.0.0-...-c50d277 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2019 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const KeyValues ctxKey = 1

KeyValues is how request values or stored/retrieved.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, val interface{}) error

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

func IsShutdown(err error) bool

IsShutdown checks to see if the shutdown error is contained in the specified error value.

func NewRequestError

func NewRequestError(err error, status int) error

NewRequestError wraps a provided error with an HTTP status code. This function should be used when handlers encounter expected errors.

func NewShutdownError

func NewShutdownError(message string) error

NewShutdownError returns an error that causes the framework to signal a graceful shutdown.

func Respond

func Respond(ctx context.Context, w http.ResponseWriter, data interface{}, statusCode int) error

Respond converts a Go value to JSON and sends it to the client.

func RespondError

func RespondError(ctx context.Context, w http.ResponseWriter, err error) error

RespondError sends an error response back to the client.

Types

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.

func (*Error) Error

func (err *Error) Error() string

Error implements the error interface. It uses the default message of the wrapped error. This is what will be shown in the services' logs.

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

type FieldError struct {
	Field string `json:"field"`
	Error string `json:"error"`
}

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) error

A Handler is a type that handles an http request within our own little mini framework.

type Middleware

type Middleware func(Handler) Handler

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.

type Router

type Router struct {
	Router *mux.Router
	// contains filtered or unexported fields
}

Router 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 New

func New(shutdown chan os.Signal, log *log.Logger, mw ...Middleware) *Router

New creates a router value that handles a set of routes for the application.

func (*Router) Handle

func (r *Router) Handle(verb, path string, handler Handler, mw ...Middleware) *mux.Route

Handle is our mechanism for mounting Handlers for a given HTTP verb and path pair, this makes for really easy, convenient routing.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *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 (*Router) SignalShutdown

func (r *Router) SignalShutdown()

SignalShutdown is used to gracefully shutdown the router when an integrity issue is identified.

type Values

type Values struct {
	TraceID    string
	Now        time.Time
	StatusCode int
}

Values represent state for each request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL