htadaptor

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2023 License: MIT Imports: 11 Imported by: 0

README

Hyper Text Adaptors

Package htadaptor provides generic domain logic adaptors for HTTP handlers. Available adaptors cover almost every possible combination of domain call shapes:

Struct Adaptor Parameter Values Return Values
UnaryFunc context, inputStruct any, error
NullaryFunc context any, error
VoidFunc context, inputStruct error

Each inputStruct must implement htadaptor.Validatable for safety.

String Adaptor Parameter Values Return Values
UnaryStringFunc context, string any, error
VoidStringFunc context, string error

Installation

go get github.com/dkotik/htadaptor@latest

Usage

mux := http.NewServeMux()
mux.Handle("/api/v1/order", htadaptor.Must(
  htadaptor.NewUnaryFuncAdaptor(myService.Order),
))

See examples folder for most common project uses.

Adaptor Options

  • WithDecoder
    • WithReadLimit
    • WithMemoryLimit
    • WithQueryValue
    • WithHeaderValue
    • WithExtractor
  • WithEncoder
  • WithLogger
    • WithSlogLogger
  • WithErrorHandler

Credits

Package includes reflection schema decoder from Gorilla toolkit.

Documentation

Overview

Package htadaptor provides generic domain logic adaptors for HTTP handlers. Adaptors come in three flavors:

1. UnaryFunc: func(context, inputStruct) (outputStruct, error) 2. NullaryFunc: func(context) (outputStruct, error) 3. VoidFunc: func(context, inputStruct) error

Each input requires implementation of Validatable for safety. Validation errors are decorated with the correct http.StatusUnprocessableEntity status code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(h http.Handler, err error) http.Handler

func NewFastFileSystem added in v0.0.3

func NewFastFileSystem(withOptions ...FastFileSystemOption) (_ http.Handler, err error)

Types

type Decoder

type Decoder interface {
	Decode(any, *http.Request) error
}

type Encoder

type Encoder interface {
	Encode(http.ResponseWriter, any) error
}

type EncoderFunc

type EncoderFunc func(http.ResponseWriter, any) error

func (EncoderFunc) Encode

func (f EncoderFunc) Encode(w http.ResponseWriter, v any) error

type Error

type Error interface {
	error
	HyperTextStatusCode() int
}

type ErrorHandler

type ErrorHandler interface {
	HandleError(http.ResponseWriter, *http.Request, error)
}

type ErrorHandlerFunc

type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)

func (ErrorHandlerFunc) HandleError

func (f ErrorHandlerFunc) HandleError(w http.ResponseWriter, r *http.Request, err error)

type FastFileSystemOption added in v0.0.3

type FastFileSystemOption func(*fastFileSystemOptions) error

func WithDefaultFastFileSystemErrorHandler added in v0.0.3

func WithDefaultFastFileSystemErrorHandler() FastFileSystemOption

func WithDefaultFastFileSystemLogger added in v0.0.3

func WithDefaultFastFileSystemLogger() FastFileSystemOption

func WithFastFileSystemErrorHandler added in v0.0.3

func WithFastFileSystemErrorHandler(e ErrorHandler) FastFileSystemOption

func WithFastFileSystemErrorHandlerFunc added in v0.0.3

func WithFastFileSystemErrorHandlerFunc(f func(http.ResponseWriter, *http.Request, error)) FastFileSystemOption

func WithFastFileSystemFile added in v0.0.3

func WithFastFileSystemFile(
	path string,
	contentType string,
	contents []byte,
) FastFileSystemOption

func WithFastFileSystemLogger added in v0.0.3

func WithFastFileSystemLogger(l RequestLogger) FastFileSystemOption

func WithFastFileSystemSlogLogger added in v0.0.3

func WithFastFileSystemSlogLogger(
	l *slog.Logger,
	successLevel slog.Leveler,
) FastFileSystemOption

type FuncType

type FuncType uint8
const (
	FuncTypeUnary FuncType = iota + 1
	FuncTypeNullary
	FuncTypeVoid
)

func Detect

func Detect(f any) (FuncType, error)

type InvalidRequestError

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

func NewInvalidRequestError

func NewInvalidRequestError(fromError error) *InvalidRequestError

func (*InvalidRequestError) Error

func (e *InvalidRequestError) Error() string

func (*InvalidRequestError) HyperTextStatusCode

func (e *InvalidRequestError) HyperTextStatusCode() int

func (*InvalidRequestError) Unwrap

func (e *InvalidRequestError) Unwrap() error

type NotFoundError added in v0.0.3

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

func (*NotFoundError) Error added in v0.0.3

func (e *NotFoundError) Error() string

func (*NotFoundError) HyperTextStatusCode added in v0.0.3

func (e *NotFoundError) HyperTextStatusCode() int

func (*NotFoundError) LogValue added in v0.0.3

func (e *NotFoundError) LogValue() slog.Value

type NullaryFuncAdaptor

type NullaryFuncAdaptor[O any] struct {
	// contains filtered or unexported fields
}

func NewNullaryFuncAdaptor

func NewNullaryFuncAdaptor[O any](
	domainCall func(context.Context) (O, error),
	withOptions ...Option,
) (*NullaryFuncAdaptor[O], error)

func (*NullaryFuncAdaptor[O]) ServeHTTP

func (a *NullaryFuncAdaptor[O]) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request,
)

func (*NullaryFuncAdaptor[O]) ServeHyperText

func (a *NullaryFuncAdaptor[O]) ServeHyperText(
	w http.ResponseWriter,
	r *http.Request,
) (err error)

type Option

type Option func(*options) error

func WithDecoder

func WithDecoder(d Decoder) Option

func WithDecoderOptions

func WithDecoderOptions(withOptions ...decoder.Option) Option

func WithDefaultDecoder

func WithDefaultDecoder() Option

func WithDefaultEncoder

func WithDefaultEncoder() Option

func WithDefaultErrorHandler

func WithDefaultErrorHandler() Option

func WithDefaultLogger

func WithDefaultLogger() Option

func WithEncoder

func WithEncoder(e Encoder) Option

func WithEncoderFunc

func WithEncoderFunc(f func(http.ResponseWriter, any) error) Option

func WithErrorHandler

func WithErrorHandler(e ErrorHandler) Option

func WithErrorHandlerFunc

func WithErrorHandlerFunc(f func(http.ResponseWriter, *http.Request, error)) Option

func WithHyperTextEncoder

func WithHyperTextEncoder(t *template.Template) Option

func WithLogger

func WithLogger(l RequestLogger) Option

func WithOptions

func WithOptions(withOptions ...Option) Option

func WithSlogLogger added in v0.0.2

func WithSlogLogger(l *slog.Logger, successLevel slog.Leveler) Option

type RequestLogger added in v0.0.2

type RequestLogger interface {
	LogRequest(*http.Request, error)
}

func NewRequestLogger added in v0.0.2

func NewRequestLogger(logger *slog.Logger, successLevel slog.Leveler) RequestLogger

func NewVoidLogger added in v0.0.2

func NewVoidLogger() RequestLogger

type RequestLoggerFunc added in v0.0.2

type RequestLoggerFunc func(*http.Request, error)

func (RequestLoggerFunc) LogRequest added in v0.0.2

func (f RequestLoggerFunc) LogRequest(r *http.Request, err error)

type StringExtractor added in v0.0.2

type StringExtractor func(*http.Request) (string, error)

type UnaryFuncAdaptor

type UnaryFuncAdaptor[
	T any,
	V Validatable[T],
	O any,
] struct {
	// contains filtered or unexported fields
}

func NewUnaryFuncAdaptor

func NewUnaryFuncAdaptor[
	T any,
	V Validatable[T],
	O any,
](
	domainCall func(context.Context, V) (O, error),
	withOptions ...Option,
) (*UnaryFuncAdaptor[T, V, O], error)

func (*UnaryFuncAdaptor[T, V, O]) ServeHTTP

func (a *UnaryFuncAdaptor[T, V, O]) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request,
)

func (*UnaryFuncAdaptor[T, V, O]) ServeHyperText

func (a *UnaryFuncAdaptor[T, V, O]) ServeHyperText(
	w http.ResponseWriter,
	r *http.Request,
) (err error)

type UnaryStringFuncAdaptor added in v0.0.2

type UnaryStringFuncAdaptor[O any] struct {
	// contains filtered or unexported fields
}

func NewUnaryStringFuncAdaptor added in v0.0.2

func NewUnaryStringFuncAdaptor[O any](
	domainCall func(context.Context, string) (O, error),
	stringExtractor StringExtractor,
	withOptions ...Option,
) (*UnaryStringFuncAdaptor[O], error)

func (*UnaryStringFuncAdaptor[O]) ServeHTTP added in v0.0.2

func (a *UnaryStringFuncAdaptor[O]) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request,
)

func (*UnaryStringFuncAdaptor[O]) ServeHyperText added in v0.0.2

func (a *UnaryStringFuncAdaptor[O]) ServeHyperText(
	w http.ResponseWriter,
	r *http.Request,
) (err error)

type Validatable

type Validatable[T any] interface {
	*T
	Validate() error
}

Validatable constrains a domain request. Validation errors are wrapped as InvalidRequestError by the adapter.

type VoidFuncAdaptor

type VoidFuncAdaptor[
	T any,
	V Validatable[T],
] struct {
	// contains filtered or unexported fields
}

func NewVoidFuncAdaptor

func NewVoidFuncAdaptor[
	T any,
	V Validatable[T],
](
	domainCall func(context.Context, V) error,
	withOptions ...Option,
) (*VoidFuncAdaptor[T, V], error)

func (*VoidFuncAdaptor[T, V]) ServeHTTP

func (a *VoidFuncAdaptor[T, V]) ServeHTTP(
	w http.ResponseWriter,
	r *http.Request,
)

func (*VoidFuncAdaptor[T, V]) ServeHyperText

func (a *VoidFuncAdaptor[T, V]) ServeHyperText(
	w http.ResponseWriter,
	r *http.Request,
) (err error)

type VoidStringFuncAdaptor added in v0.0.2

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

func NewVoidStringFuncAdaptor added in v0.0.2

func NewVoidStringFuncAdaptor(
	domainCall func(context.Context, string) error,
	stringExtractor StringExtractor,
	withOptions ...Option,
) (*VoidStringFuncAdaptor, error)

func (*VoidStringFuncAdaptor) ServeHTTP added in v0.0.2

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

func (*VoidStringFuncAdaptor) ServeHyperText added in v0.0.2

func (a *VoidStringFuncAdaptor) ServeHyperText(
	w http.ResponseWriter,
	r *http.Request,
) (err error)

Directories

Path Synopsis
Package chivalue provides value extractors for Chi Router.
Package chivalue provides value extractors for Chi Router.
chivalues module
Package decoder provides configurable HTTP form body decoding that depends on Gorilla's schema package which does not contaminate objects with URL query parameters unlike the standard library.
Package decoder provides configurable HTTP form body decoding that depends on Gorilla's schema package which does not contaminate objects with URL query parameters unlike the standard library.
schema
Package schema fills a struct with form values.
Package schema fills a struct with form values.
examples
basic
Package main demonstrates routing directly to domain function calls.
Package main demonstrates routing directly to domain function calls.
extract
chivalues Module
TODO: move this into root package.
TODO: move this into root package.

Jump to

Keyboard shortcuts

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