muxhttp

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package muxhttp provides mux, middleware, handler with regards to routing capabilities

Index

Constants

View Source
const KeyValues ctxKey = 1

KeyValues is how request values or stored/retrieved.

Variables

This section is empty.

Functions

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 service encounter expected errors.

func NewShutdownError

func NewShutdownError(message string) error

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

Types

type ErrorRequest

type ErrorRequest struct {
	Err    error
	Status int
	Fields []FieldError
}

ErrorRequest is used to pass an error during the request through the application with web specific context.

func (*ErrorRequest) Error

func (err *ErrorRequest) Error() string

ErrorField 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

Handler is the signature used by all in this jsonfmt.

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 {
	// contains filtered or unexported fields
}

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

func NewRouter(shutdownCh chan os.Signal, log *log.Logger, mw ...Middleware) *Router

NewRouter constructs an Router to handle a set of routes. Any Middleware provided will be ran for every request.

func (*Router) Handle

func (a *Router) 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 cli in a uniform way.

func (*Router) ServeHTTP

func (a *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (*Router) SignalShutdown

func (a *Router) SignalShutdown()

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

type Values

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

Values carries information about each request.

Jump to

Keyboard shortcuts

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