web

package
v0.0.0-...-faaee1e Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package web contains gluecode for abstracting observability, middlewares and error handling around http Handlers

Index

Constants

View Source
const KeyValues ctxKey = 1

KeyValues is how request metadata (type Values) are stored/retrieved.

Variables

This section is empty.

Functions

func Decode

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

Decode gets the JSON value from the request body and decode it. 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 type 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 Param

func Param(r *http.Request, key string) string

Func Param extract request param from the URL called based on the provided key

func Respond

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

Encode business layer outputs to send back to clients.

func RespondError

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

RespondError sends an error response back to clients.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

Type App is the entrypoint into the web application, it configures context for http handlers and hooks up os.Signal from application inner layers. This can be extended for further behaviors.

func NewApp

func NewApp(shutdown chan os.Signal, mw ...Middleware) *App

Factory method for creating concrete App that handles http routes handling

func (*App) Handle

func (a *App) Handle(method string, path string, handler Handler, mw ...Middleware)

Handle encapsulates concrete http.HandleFunc calls to abstract requests observability and error handling

func (*App) ServeHTTP

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

ServeHTTP is the entry point for all http traffic and allows the opentelemetry mux to run first to handle tracing. The opentelemetry mux then calls the application mux to handle application traffic.

func (*App) SignalShutdown

func (a *App) SignalShutdown()

SignalShutdown is for gracefully shutdown the application process hooking up OS signals

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 specific context.

func (*Error) Error

func (err *Error) Error() string

Error method 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 for API responses.

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

Type Handler is an adapter to allow the use of custom method signature as native http.HandlerFunc

type Middleware

type Middleware func(Handler) Handler

Middleware function helps to run some code before and/or after another Handler.

type Values

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

Values represent metadata attached to requests for debugging purposes.

Jump to

Keyboard shortcuts

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