midware

package
v0.0.0-...-d4dc666 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package midware provides a way to convert a MidwareFunc to an http.HandlerFunc. MidwareFunc is not a literal type but is any func of the form - func(w http.ResponseWriter, r *http.Request, data *struct{...})

Midware is used to inject values into data using reflection.

Index

Constants

This section is empty.

Variables

View Source
var (
	HttpHandlerType = filter.NumInEq(3).
		And(filter.InType(0, httptype.ResponseWriter)).
		And(filter.InType(1, httptype.Request))
)
View Source
var (
	Vars = mux.Vars
)

Vars references mux.Vars. It is left exposed for testing.

Functions

func GetWR

func GetWR(args []reflect.Value) (w http.ResponseWriter, r *http.Request)

func NewDecoder

func NewDecoder(d lhttp.RequestDecoder, fieldName string) linject.Field

NewDecoder creates a magic midware that decodes data from the request and sets it to a field on the magic data type. The field must be a pointer to a struct.

func NewField

func NewField(fsi linject.FieldInitilizer, fieldName string) linject.Field

func NewRedirect

func NewRedirect(fieldName string) linject.Field

NewRedirect creates a Redirect Initilizer. It should be the last Initilizer in the Midware.

func URL

func URL(segment, fieldName string) linject.Field

URL creates a magic Initilizer to extract a segment from the URL by name and set it to a magic data field by fieldName.

Types

type DecoderInitilizer

type DecoderInitilizer struct {
	lhttp.RequestDecoder
}

DecoderInitilizer fulfills FieldSetterInitilizer.

func (DecoderInitilizer) InitilizeField

Initilize fulfills FieldSetterInitilizer. It validates that the Type t is a pointer to a struct.

type Initilizer

type Initilizer interface {
	Initilize(reflect.Type) Injector
}

type Injector

type Injector interface {
	Inject(w http.ResponseWriter, r *http.Request, dst reflect.Value) (func([]reflect.Value), error)
}

type Midware

type Midware struct {
	linject.Initilizers
}

Midware holds the Initilizers that build the DataInserters for dataType.

func New

func New(initilizers ...linject.Initilizer) *Midware

New creates a set of midware initilizers that can be used to convert midwareFuncs to http.HandlerFuncs.

func (*Midware) Handle

func (m *Midware) Handle(fn any) http.HandlerFunc

func (*Midware) Inits

func (m *Midware) Inits(initilizers ...Initilizer) *Midware

type Redirect

type Redirect struct{}

Redirect is solves a side effect of the callbacks. When http.Redirect is invoked it prevents cookies from being written (and possibly other headers). Putting Redirect at the bottom of midware.NewMagic allows redirects to be set and invoked at the end of the call.

func (Redirect) InitilizeField

func (rd Redirect) InitilizeField(fn linject.FuncType, t reflect.Type) linject.FieldInjector

Initilize fulfills FieldSetterInitilizer.

func (Redirect) InjectField

func (rd Redirect) InjectField(args []reflect.Value, field reflect.Value) (func([]reflect.Value), error)

Set fulfills FieldSetter by setting field to a value from the URL using mux.Vars.

type URLFieldSetter

type URLFieldSetter struct {
	Var string
}

URLFieldSetter references a Var in gorilla/mux.Vars. It fulfills FieldSetterInitilizer and FieldSetter. It is used to extract values from a URL and set them in the magic data field.

type WebSocket

type WebSocket struct {
	Upgrader       websocket.Upgrader
	ToBuf, FromBuf int
	lhttp.ErrHandler
}

WebSocket can perform two different midware operations. It can attach a *websocket.Conn directly or it can attach a set of []byte channels connected to a websocket.

func NewWebSocket

func NewWebSocket() WebSocket

// NewWebSocket with default buffer sizes.

func (WebSocket) Initilizer

func (ws WebSocket) Initilizer(to, from, conn string) Initilizer

// ChannelInitilizer creates an Initilizer that will abstract the websocket to // a pair of []byte channels.

Jump to

Keyboard shortcuts

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