web

package
v0.0.0-...-bbff7f4 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2022 License: MIT Imports: 18 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(request *http.Request, val interface{}) error

Decode reads the body of a 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 is used when a know error condition is encountered.

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, writer http.ResponseWriter, val interface{}, statusCode int) error

Respond marshals a value to JSON and sends it to the client.

func RespondError

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

RespondError knows how to handle errors going out to the client.

Types

type Error

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

Error is used to add web information to a request error.

func (*Error) Error

func (e *Error) Error() string

type ErrorResponse

type ErrorResponse struct {
	Error  string       `json:"error"`
	Fields []FieldError `json:"fields,omitempty"`
}

ErrorResponse how we respond to clients when something goes wrong.

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

Handler is the signature that all application handlers will implement.

type Middleware

type Middleware func(handler 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 {
	Log *log.Logger
	// contains filtered or unexported fields
}

Router is the entry point for all web applications.

func NewRouter

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

NewRouter knows how to construct internal state for a Router.

func (*Router) Handler

func (router *Router) Handler(method, pattern string, h Handler, mw ...Middleware)

Handler connects a method and URL pattern to a particular application handler.

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*Router) SignalShutdown

func (router *Router) SignalShutdown()

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

type Values

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

Values carries information about each request.

Jump to

Keyboard shortcuts

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