oakrouter

package
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: 9 Imported by: 0

Documentation

Overview

Package oakrouter provides an ugly oakhttp.Handler multiplexer as a hashmap router paired with a http.Method switch.

The pairing is entirely sufficient for building small services, assuming that most endpoints should extract request values from request.Body only without touching URL.Path or the HTTP header.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPathEnd        = &MatchError{cause: errors.New("no more path segments left")}
	ErrPathNotEnd     = &MatchError{cause: errors.New("path has more available segments")}
	ErrTrailingSlash  = &MatchError{cause: errors.New("trailing slash")}
	ErrDoubleSlash    = &MatchError{cause: errors.New("two consequitive slashes in path")}
	ErrPrefixMismatch = &MatchError{cause: errors.New("required prefix is does not match request URL path")}
)

Functions

func ChompPath added in v0.0.5

func ChompPath(p string) (head, tail string)

ChompPath splits off the first segment of the request URL path as head from the remaining tail. The head segment never contains a slash. Path is not cleaned because it creates ambiguities. The same source may be fetched by using different paths.

This function allows building branching routing handlers without having to rely on a multiplexer. Chomp multiple times in the same routing handler as needed. Before passing request to the next handler, overwrite [http.request.URL.Path].

Based on ShiftPath: https://blog.merovius.de/posts/2017-06-18-how-not-to-use-an-http-router/ See also: https://github.com/benhoyt/go-routing/blob/master/shiftpath/route.go And: https://benhoyt.com/writings/go-routing/

func New

func New(withOptions ...Option) (oakhttp.Handler, error)

func NewMapSwitch added in v0.0.5

func NewMapSwitch() (oakhttp.Handler, error)

func NewRedirect added in v0.0.5

func NewRedirect(URL string, statusCode int) oakhttp.Handler

func TailTrailingSlashRedirectOrNotFound added in v0.0.5

func TailTrailingSlashRedirectOrNotFound(w http.ResponseWriter, r *http.Request, tail string) error

Types

type Map added in v0.0.5

type Map map[string]oakhttp.Handler

type MatchError added in v0.0.5

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

func (*MatchError) Error added in v0.0.5

func (m *MatchError) Error() string

func (*MatchError) HTTPStatusCode added in v0.0.5

func (m *MatchError) HTTPStatusCode() int

func (*MatchError) Unwrap added in v0.0.5

func (m *MatchError) Unwrap() error

type Matcher added in v0.0.5

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

func NewMatcher added in v0.0.5

func NewMatcher(p string) *Matcher

func (*Matcher) MatchBytes added in v0.0.5

func (p *Matcher) MatchBytes() ([]byte, error)

func (*Matcher) MatchEnd added in v0.0.5

func (p *Matcher) MatchEnd() error

func (*Matcher) MatchInt added in v0.0.5

func (p *Matcher) MatchInt() (int, error)

func (*Matcher) MatchSlug added in v0.0.5

func (p *Matcher) MatchSlug() (string, error)

func (*Matcher) MatchString added in v0.0.5

func (p *Matcher) MatchString() (string, error)

func (*Matcher) MatchUint added in v0.0.5

func (p *Matcher) MatchUint() (uint, error)

func (*Matcher) Skip added in v0.0.5

func (p *Matcher) Skip() (err error)

type Option

type Option func(*options) error

func WithMiddleware

func WithMiddleware(mw ...oakhttp.Middleware) Option

WithMiddleware wraps handler with oakhttp.Middleware. This option can be used multiple times. The final middleware chain is applied in reverse order to preserve logical order. For example, WithMiddleware(first, second, third) provides a handler which is equivalent to manually calling first(second(third(handler))) without this option.

func WithPathPrefix

func WithPathPrefix(p string) Option

func WithPathPrefixCutFromRequest

func WithPathPrefixCutFromRequest() Option

func WithRequestHandler

func WithRequestHandler[
	T any,
	R oakhttp.ValidatableNormalizable[T],
](
	h func(context.Context, R) error,
	withAdaptorOptions ...oakhttp.Option,
) Option

func WithRequestResponseHandler

func WithRequestResponseHandler[
	T any,
	R oakhttp.ValidatableNormalizable[T],
	P any,
](
	h func(context.Context, R) (P, error),
	withAdaptorOptions ...oakhttp.Option,
) Option

func WithRoute

func WithRoute(path string, h oakhttp.Handler) Option

func WithSlugRequestHandler

func WithSlugRequestHandler(
	h func(context.Context, string) error,
) Option

func WithoutTrailingSlashRedirects added in v0.0.5

func WithoutTrailingSlashRedirects() Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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