oakhttp

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2023 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DecoderJSON = func(v any, r io.Reader) error {
	return json.NewDecoder(r).Decode(&v)
}
View Source
var EncoderJSON = func(w http.ResponseWriter, v any) error {
	w.Header().Set("Context-Type", "application/json")
	return json.NewEncoder(w).Encode(v)
}
View Source
var ErrorTemplate string

Functions

This section is empty.

Types

type Cache added in v0.0.3

type Cache interface {
	Set(ctx context.Context, key, value string) (err error)
	Get(ctx context.Context, key string) (value string, err error)
}

type Decoder added in v0.0.3

type Decoder func(any, io.Reader) error

type DomainRequest added in v0.0.3

type DomainRequest[T any, P ValidatableNormalizable[T]] func(context.Context, P) error

type DomainRequestResponse added in v0.0.3

type DomainRequestResponse[T any, P ValidatableNormalizable[T], O any] func(context.Context, P) (O, error)

type Encoder added in v0.0.3

type Encoder func(http.ResponseWriter, any) error

type Error added in v0.0.2

type Error interface {
	error
	HTTPStatusCode() int
}

type ErrorHandler added in v0.0.3

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

func NewErrorHandlerHTML added in v0.0.3

func NewErrorHandlerHTML(l *slog.Logger, t *template.Template) ErrorHandler

func NewErrorHandlerJSON added in v0.0.3

func NewErrorHandlerJSON(l *slog.Logger) ErrorHandler

type Handler

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

func NewComplexRequestAdaptor added in v0.0.3

func NewComplexRequestAdaptor[
	T any,
	R ValidatableNormalizable[T],
](
	handler DomainRequest[T, R],
	factory RequestFactory[T, R],
) (Handler, error)

func NewComplexRequestResponseAdaptor added in v0.0.3

func NewComplexRequestResponseAdaptor[
	T any,
	R ValidatableNormalizable[T],
	P any,
](
	handler DomainRequestResponse[T, R, P],
	factory RequestFactory[T, R],
	encoder Encoder,
) (Handler, error)

func NewPanicRecoveryHandler added in v0.0.5

func NewPanicRecoveryHandler(next Handler) Handler

NewPanicRecoveryHandler protects handlers from panics by converting them to errors.

See another variant: https://github.com/go-chi/chi/blob/v5.0.8/middleware/recoverer.go

func NewPanicRecoveryMiddleware added in v0.0.5

func NewPanicRecoveryMiddleware(next Handler) Handler

func NewRequestAdaptor added in v0.0.3

func NewRequestAdaptor[
	T any,
	R ValidatableNormalizable[T],
](
	handler DomainRequest[T, R],
	withOptions ...Option,
) (Handler, error)

func NewRequestResponseAdaptor added in v0.0.3

func NewRequestResponseAdaptor[
	T any,
	R ValidatableNormalizable[T],
	P any,
](
	handler DomainRequestResponse[T, R, P],
	withOptions ...Option,
) (Handler, error)

func NewSlugRequestAdaptor added in v0.0.3

func NewSlugRequestAdaptor(
	h func(context.Context, string) error,
) (Handler, error)

func NewSlugRequestResponseAdaptor added in v0.0.3

func NewSlugRequestResponseAdaptor[R any](
	h func(context.Context, string) (R, error),
	withOptions ...Option,
) (Handler, error)

type HashableByPage

type HashableByPage[T any] interface {
	*T
	Hash(page, perPage int) string
}

type InvalidRequestError added in v0.0.3

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

InvalidRequestError represents

func NewInvalidRequestError added in v0.0.3

func NewInvalidRequestError(fromError error) *InvalidRequestError

func (*InvalidRequestError) Error added in v0.0.3

func (e *InvalidRequestError) Error() string

func (*InvalidRequestError) HTTPStatusCode added in v0.0.3

func (e *InvalidRequestError) HTTPStatusCode() int

func (*InvalidRequestError) Unwrap added in v0.0.3

func (e *InvalidRequestError) Unwrap() error

type Middleware

type Middleware func(Handler) Handler

func NewErrorHandlerMiddleware added in v0.0.3

func NewErrorHandlerMiddleware(h ErrorHandler) Middleware

type NotFoundError

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

func NewNotFoundError

func NewNotFoundError(resource string) *NotFoundError

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

func (*NotFoundError) HTTPStatusCode

func (e *NotFoundError) HTTPStatusCode() int

type Option added in v0.0.3

type Option func(*options) error

func WithDecoder added in v0.0.3

func WithDecoder(d Decoder) Option

func WithDefaultOptions added in v0.0.3

func WithDefaultOptions() Option

func WithEncoder added in v0.0.3

func WithEncoder(e Encoder) Option

func WithEncodingJSON added in v0.0.3

func WithEncodingJSON() Option

func WithReadLimit added in v0.0.3

func WithReadLimit(l int64) Option

type Page

type Page[T any] struct {
	Items []*T
	Page  int
	Total int
}

type PageRequest

type PageRequest[T any, H HashableByPage[T]] struct {
	Query   H
	Page    int
	PerPage int
}

type PanicError added in v0.0.5

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

func (*PanicError) Error added in v0.0.5

func (e *PanicError) Error() string

func (*PanicError) HTTPStatusCode added in v0.0.5

func (e *PanicError) HTTPStatusCode() int

func (*PanicError) LogValue added in v0.0.5

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

type RequestFactory

type RequestFactory[T any, P ValidatableNormalizable[T]] func(
	w http.ResponseWriter,
	r *http.Request,
) (P, error)

type ValidatableNormalizable added in v0.0.3

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

ValidatableNormalizable constrains a domain request. Validation and normalization errors will be wrapped as InvalidRequestError by the adapter.

Directories

Path Synopsis
botswat module
turnstile
Package turnstile creates a secure by default humanity verifier backed by Cloudflare's Turnstile service.
Package turnstile creates a secure by default humanity verifier backed by Cloudflare's Turnstile service.
Package oakclient provides a secure HTTP client.
Package oakclient provides a secure HTTP client.
oakoidc module
Package oakrouter provides an ugly [oakhttp.Handler] multiplexer as a hashmap router paired with a [http.Method] switch.
Package oakrouter provides an ugly [oakhttp.Handler] multiplexer as a hashmap router paired with a [http.Method] switch.
Package oakserver provides a standard library [http.Server] with conventional production defaults and a smooth configuration interface.
Package oakserver provides a standard library [http.Server] with conventional production defaults and a smooth configuration interface.

Jump to

Keyboard shortcuts

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