httpbind

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Identity

func Identity[I any](in *I) (*I, error)

func IndirectJSONInput

func IndirectJSONInput[P any, I any](t func(*P) (*I, error)) func(r *http.Request) (*I, error)

IndirectJSONINput parses r's Body into a *P before passing it to a user-defined transformer function that produces a *I.

func ParseJSON

func ParseJSON[P any](r *http.Request) (*P, error)

ParseJSON parses r's Body into a *P

func Transform

func Transform[I any, O any](getInput func(r *http.Request) (*I, error), t func(*I) (*O, error)) func(r *http.Request) (*O, error)

Transform returns a function that reads input from an HTTP request as an *I before passing it to a user-defined transformer function that produces an *O.

func WriteJSON

func WriteJSON[T any](w http.ResponseWriter, val *T)

WriteJSON writes a *T to w as JSON

func Zero

func Zero[I any](r *http.Request) (*I, error)

Types

type Invoker

type Invoker[Tx operator.Transaction, I any, O any] struct {
	// contains filtered or unexported fields
}

Invoker acts as a configuration point when binding operations to HTTP endpoints. Use its With* functions to customise input, output, and error behaviour, then call Go() to invoke the operation.

func Bind

func Bind[Tx operator.Transaction, I any, O any](
	hub *operator.Hub[Tx],
	op func(*operator.OpContext[Tx], *I) (*O, error),
) *Invoker[Tx, I, O]

Bind() creates an an Invoker binding the operation to an HTTP endpoint The returned Invoker can be further customised before finally calling Go()

func BindTx

func BindTx[Tx operator.Transaction, I any, O any](
	hub *operator.Hub[Tx],
	op func(*operator.OpContext[Tx], Tx, *I) (*O, error),
) *Invoker[Tx, I, O]

BindTx() creates an an Invoker binding the operation to an HTTP endpoint The returned Invoker can be further customised before finally calling Go()

func (*Invoker[Tx, I, O]) Go

func (i *Invoker[Tx, I, O]) Go(w http.ResponseWriter, r *http.Request)

Invoke the bound operation in the context of the supplied HTTP request

func (*Invoker[Tx, I, O]) WithContext

func (i *Invoker[Tx, I, O]) WithContext(ctx context.Context) *Invoker[Tx, I, O]

WithContext() sets a static context for the operation

func (*Invoker[Tx, I, O]) WithContextFunc

func (i *Invoker[Tx, I, O]) WithContextFunc(fn func(*http.Request) context.Context) *Invoker[Tx, I, O]

WithContextFn() sets fn as a context factory the operation. This can be used, for an example, to use the HTTP request's context as the operation context. (in most cases this is undesirable, however).

func (*Invoker[Tx, I, O]) WithErrorMapper

func (i *Invoker[Tx, I, O]) WithErrorMapper(fn func(w http.ResponseWriter, err error)) *Invoker[Tx, I, O]

Register an error mapper for writing an error to the HTTP response.

The error provided to the callback wraps both the source error, and one of either operr.ErrInputMappingFailed or operr.ErrOperationFailed, to indicate in which phase the error occurred.

Since you will likely use the same error mapper for every operation, to avoid registering the mapper each time, it is common to wrap Bind() and BindTx() to attach your preferred handler automatically.

func (*Invoker[Tx, I, O]) WithInputMapper

func (i *Invoker[Tx, I, O]) WithInputMapper(fn func(*http.Request) (*I, error)) *Invoker[Tx, I, O]

WithInputMapper() registers the binding's input mapper

func (*Invoker[Tx, I, O]) WithJSONOutput

func (i *Invoker[Tx, I, O]) WithJSONOutput(fn func(w http.ResponseWriter, o *O) any) *Invoker[Tx, I, O]

WithJSONOutput() is a shortcut method for the common pattern of writing an operation's output directly as JSON

func (*Invoker[Tx, I, O]) WithJSONOutputFunc

func (i *Invoker[Tx, I, O]) WithJSONOutputFunc(fn func(w http.ResponseWriter, o *O)) *Invoker[Tx, I, O]

WithJSONOutputFunc() is a shortcut method for the common pattern of transforming an operation's output into JSON

func (*Invoker[Tx, I, O]) WithOutputMapper

func (i *Invoker[Tx, I, O]) WithOutputMapper(fn func(w http.ResponseWriter, o *O)) *Invoker[Tx, I, O]

WithOutputMapper() registers the binding's output mapper

Jump to

Keyboard shortcuts

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