web

package
v0.0.0-...-e400c07 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: Apache-2.0 Imports: 8 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 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. If the value implements a validate function, it is executed.

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID returns the trace id from the context.

func Param

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

Param returns the web call parameters from the request.

func Respond

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

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

func SetStatusCode

func SetStatusCode(ctx context.Context, statusCode int)

SetStatusCode sets the status code back into the context.

Types

type App

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

App is the entrypoint into the 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 NewApp

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

NewApp creates an App instance using the chi router.

func (*App) Handle

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

Handle sets a handler function for a given HTTP method and path pair to the application server mux.

func (*App) ServeHTTP

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

ServeHTTP method implements the http.Handler interface for App. It's the entry point for all http traffic.

func (*App) SignalShutdown

func (a *App) SignalShutdown()

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

type Handler

type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) error

Handler is a type definition that handles a http request within the mini framework.

type Middleware

type Middleware func(Handler) Handler

Middleware is a type definition that expects and returns a Handler. It allows you to define middleware functions that can intercept HTTP requests, perform actions before or after passing control to another handler, modify the request or response, or short-circuit the request by not calling the next handler.

type Values

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

Values represents state for each request

Jump to

Keyboard shortcuts

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