web

package
v0.0.0-...-b891794 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, val any) error

Decode decodes the body of an HTTP request and stores the result in dst.

func GetTime

func GetTime(ctx context.Context) time.Time

GetTime returns the time from the context.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID returns the trace id from the context.

func IsError

func IsError(err error) bool

IsError checks if an error of type Error exists.

func IsShutdown

func IsShutdown(err error) bool

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

func NewError

func NewError(err error, status int) error

NewError wraps a provided error with an HTTP status code. This function should be used as fallback when handlers encounter expected errors i.e., if it was not a validation error, auth error, or db error, etc.

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, name string) string

Param returns the web call parameters from the request.

func RespondJson

func RespondJson(ctx context.Context, w http.ResponseWriter, data any, statusCode int) error

RespondJson wraps the return the value as {data: value} according to the status code and sends json to the response writer

func RespondJsonPlain

func RespondJsonPlain(ctx context.Context, w http.ResponseWriter, data any, statusCode int) error

RespondJsonPlain sends json to the response writer without wrapping

func SetStatusCode

func SetStatusCode(ctx context.Context, statusCode int)

SetStatusCode sets the status code back into the context.

Types

type App

type App struct {
	*httptreemux.ContextMux
	// contains filtered or unexported fields
}

App is the entrypoint into our application and what configures our context object for each of our http handlers.

func NewApp

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

NewApp creates an App value that handle a set of routes for the application.

func (*App) AddGroup

func (a *App) AddGroup(rg ...RouteGroup)

AddGroup accepts a list of RouteGroup and registers them with the App

func (*App) GET

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

func (*App) Handle

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

func (*App) PATCH

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

func (*App) POST

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

func (*App) SignalShutdown

func (a *App) SignalShutdown()

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

type Error

type Error struct {
	Err    error
	Status int
}

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

func GetError

func GetError(err error) *Error

GetError returns a copy of the Error pointer.

func (*Error) Error

func (re *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 ErrorDocument

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

ErrorDocument is the form used for API responses from failures in the API.

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 App different than the http.Handler in that it accepts context as the first parameter and returns an error

type Middleware

type Middleware func(Handler) Handler

Middleware is a function designed to run some code before and/or after another Handler.

type RouteGroup

type RouteGroup interface {
	Register(*App)
}

RouteGroup defines the behavior that binds routes to App

type Values

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

Values represent state for each request.

func GetValues

func GetValues(ctx context.Context) *Values

GetValues returns the values from the context.

Jump to

Keyboard shortcuts

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