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 ¶
- Variables
- func ChompPath(p string) (head, tail string)
- func New(withOptions ...Option) (oakhttp.Handler, error)
- func NewMapSwitch() (oakhttp.Handler, error)
- func NewRedirect(URL string, statusCode int) oakhttp.Handler
- func TailTrailingSlashRedirectOrNotFound(w http.ResponseWriter, r *http.Request, tail string) error
- type Map
- type MatchError
- type Matcher
- type Option
- func WithMiddleware(mw ...oakhttp.Middleware) Option
- func WithPathPrefix(p string) Option
- func WithPathPrefixCutFromRequest() Option
- func WithRequestHandler[T any, R oakhttp.ValidatableNormalizable[T]](h func(context.Context, R) error, withAdaptorOptions ...oakhttp.Option) Option
- func WithRequestResponseHandler[T any, R oakhttp.ValidatableNormalizable[T], P any](h func(context.Context, R) (P, error), withAdaptorOptions ...oakhttp.Option) Option
- func WithRoute(path string, h oakhttp.Handler) Option
- func WithSlugRequestHandler(h func(context.Context, string) error) Option
- func WithoutTrailingSlashRedirects() Option
Constants ¶
This section is empty.
Variables ¶
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
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 NewMapSwitch ¶ added in v0.0.5
func TailTrailingSlashRedirectOrNotFound ¶ added in v0.0.5
Types ¶
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 (*Matcher) MatchBytes ¶ added in v0.0.5
func (*Matcher) MatchString ¶ added in v0.0.5
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 WithPathPrefixCutFromRequest ¶
func WithPathPrefixCutFromRequest() Option
func WithRequestHandler ¶
func WithSlugRequestHandler ¶
func WithoutTrailingSlashRedirects ¶ added in v0.0.5
func WithoutTrailingSlashRedirects() Option