web

package
v0.0.0-...-3d2c863 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: 10 Imported by: 6

Documentation

Overview

Package web contains a small web framework extension.

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

Types

type App

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

App is the entrypoint into our 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(log Logger, tracer trace.Tracer, mw ...MidHandler) *App

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

func (*App) EnableCORS

func (a *App) EnableCORS(mw MidHandler)

EnableCORS enables CORS preflight requests to work in the middleware. It prevents the MethodNotAllowedHandler from being called. This must be enabled for the CORS middleware to work.

func (*App) Handle

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

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

func (*App) HandleNoMiddleware

func (a *App) HandleNoMiddleware(method string, group string, path string, handler Handler)

HandleNoMiddleware sets a handler function for a given HTTP method and path pair to the application server mux. Does not include the application middleware or OTEL tracing.

func (*App) ServeHTTP

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

ServeHTTP implements the http.Handler interface. It's 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. This was set up on line 44 in the NewApp function.

type Handler

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

Handler represents a function that handles a http request within our own little mini framework.

type Logger

type Logger func(ctx context.Context, msg string, v ...any)

Logger represents a function that will be called to add information to the logs.

type MidHandler

type MidHandler func(Handler) Handler

MidHandler is a handler function designed to run code before and/or after another Handler. It is designed to remove boilerplate or other concerns not direct to any given app Handler.

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