Documentation ¶
Index ¶
- Variables
- func NewContext(req Request, res Response) *builtinContext
- func NewContextFromStdlib(w http.ResponseWriter, req *http.Request) *builtinContext
- func NewErrorContext(ctx Context, err error) *builtinErrorContext
- func RegisterHandlerArgFactory[T any](factory HandlerIntrospectorArgFactory)
- type Context
- type ErrorContext
- type Group
- type Handleable
- type Handler
- type HandlerFunc
- type HandlerIntrospector
- type HandlerIntrospectorArgFactory
- type MiddlewareHandlerFunc
- type Request
- type Response
- type Router
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorNotAHandler is returned when an attempt to get a handler from an interface{} failed. ErrorNotAHandler = errors.New("Given handler value is not of the type router.Handler") )
Functions ¶
func NewContext ¶
func NewContextFromStdlib ¶
func NewContextFromStdlib(w http.ResponseWriter, req *http.Request) *builtinContext
func NewErrorContext ¶ added in v0.0.3
func RegisterHandlerArgFactory ¶
func RegisterHandlerArgFactory[T any](factory HandlerIntrospectorArgFactory)
RegisterHandlerArgFactory will register a factory function for the given generic type which will enable the generic type to be automatically resolved in all handlers.
Types ¶
type Context ¶
type Context interface { // Request returns the request object for the current context Request() Request // Response returns the response object for the current context Response() Response // Response is the mojito implementation of a response which wraps around a regular // http.ResponseWriter object JSON(body interface{}) error // PrettyJSON writes any object to the response body as pretty JSON PrettyJSON(body interface{}) error // Metadata contains the metadata about the current context Metadata() structures.Map[string, interface{}] // String will write a string to the response body String(body string) error Completed() bool CompletedChan() chan bool // contains filtered or unexported methods }
Context contains the request and response objects of a request.
type ErrorContext ¶ added in v0.0.3
type ErrorContext interface { Context // Error returns the error object for the current context Error() error }
ErrorContext contains context for error handlers.
type Group ¶
type Group interface { // GET will add a route to this route group for the get method GET(path string, handler interface{}) // HEAD will add a route to this route group for the head method HEAD(path string, handler interface{}) // POST will add a route to this route group for the post method POST(path string, handler interface{}) // PUT will add a route to this route group for the put method PUT(path string, handler interface{}) // DELETE will add a route to this route group for the delete method DELETE(path string, handler interface{}) // CONNECT will add a route to this route group for the connect method CONNECT(path string, handler interface{}) // OPTIONS will add a route to this route group for the options method OPTIONS(path string, handler interface{}) // TRACE will add a route to this route group for the trace method TRACE(path string, handler interface{}) // PATCH will add a route to this route group for the patch method PATCH(path string, handler interface{}) // WithMiddleware will add a middleware to the route group WithMiddleware(handler interface{}) // WithRoute will add a new route with the given http method to the router group WithRoute(method string, path string, handler interface{}) // Middleware will return a list of potential middlewares that were registered on this router group Middleware() []interface{} // ApplyToRouter will attempt to merge all middlewares with the collected route handlers // and then attempt to register the routes on the router with a given prefix. // Will return an error if any of the steps throw an error ApplyToRouter(router Router, prefix string) error }
Router defines the minimal surface of a router group compatible with this package
type Handleable ¶ added in v0.0.8
type Handleable interface {
ToHandler() interface{}
}
Handleable defines the interface for a structure that can be used to create a handler
type Handler ¶
type Handler interface { // AddMiddleware adds a middleware into the chain AddMiddleware(middleware interface{}) error // Introspection will return the introspected information about the handler Introspection() HandlerIntrospector // HandlerFunc will generate the original handlers function // without any chained middlewares HandlerFunc() HandlerFunc // Serve is the handler func that executes the handler and all the middlewares Serve(ctx Context) error // Value returns the reflection value of the handler func Value() reflect.Value }
Handler defines a mojito handler
func GetHandler ¶
GetHandler will try to cast the given value into a handler, but will fail if the value is not of type (*)mojito.Handler
func GetOrCreateHandler ¶
func NewHandler ¶
NewHandler will introspect the given handler and, if valid, return a new handler instance that can serve a route
type HandlerFunc ¶
HandlerFunc describes the method signature of a mojito handler function that can process an incoming mojito.Requests. A handler func masks all middleware and dependency injection behind a simple, easy to use interface
func HandlerChain ¶
func HandlerChain(f HandlerFunc, middleware MiddlewareHandlerFunc) HandlerFunc
HandlerChain will chain a handler func and a middleware together to form a new, single handler func
type HandlerIntrospector ¶
type HandlerIntrospector interface { // CanError returns true when the handler func is returning error CanError() bool // FactoryMap returns the factories mapped to the argument positions of the handler FactoryMap() map[int]HandlerIntrospectorArgFactory // IsStdlibMiddleware returns true when the handler func is a stdlib middleware IsStdlibMiddleware() bool // Type returns the handler reflection type Type() reflect.Type }
HandlerIntrospector is a structure that contains all introspected details about a (potential) handler
func IntrospectHandler ¶
func IntrospectHandler(handler interface{}) (HandlerIntrospector, error)
type HandlerIntrospectorArgFactory ¶
type HandlerIntrospectorArgFactory func(ctx Context, next HandlerFunc) reflect.Value
HandlerArgFactory defines the signature of handler argument factories
type MiddlewareHandlerFunc ¶
MiddlewareHandlerFunc is a special kind of HandlerFunc, that receives the next handler in line as its third parameter.
type Request ¶
type Request interface { // GetRequest returns the underlying http.Request object GetRequest() *http.Request // Request replaces the underlying http.Request object SetRequest(req *http.Request) // Param returns the route parameter or empty string if not found Param(name string) string // ParamOrDefault returns the route parameter or a custom default if not found ParamOrDefault(name string, def string) string // SetParams will set the routep params for this request SetParams(params map[string]string) // HasContentType determines whether a request has a given mime type as its content type HasContentType(mimetype string) bool }
func NewRequest ¶
NewRequest will create a new instance of a mojito request for the given http.Request object
type Response ¶
type Response interface { http.ResponseWriter // GetWriter returns the underlying response writer instance GetWriter() http.ResponseWriter // SetWriter replaces the underlying response writer instance SetWriter(res http.ResponseWriter) }
func NewResponse ¶
func NewResponse(res http.ResponseWriter) Response
NewResponse will create a new instance of a mojito response for the given http.ResponseWriter object
type Router ¶
type Router interface { // WithGroup will create a new route group for the given prefix WithGroup(prefix string, callback func(group Group)) error // WithRoute will add a new route with the given RouteMethod to the router WithRoute(method string, path string, handler interface{}) error // WithNotFound will set the not found handler for the router WithNotFoundHandler(handler interface{}) error // WithMethodNotAllowedHandler will set the not allowed handler for the router WithMethodNotAllowedHandler(handler interface{}) error // WithErrorHandler will set the error handler for the router WithErrorHandler(handler interface{}) error // WithMiddleware will add a middleware to the router WithMiddleware(handler interface{}) error // ListenAndServe will start an HTTP webserver on the given address ListenAndServe(address string) error // Shutdown will gracefully shutdown the router Shutdown() error }
Router defines the minimal surface of a router compatible with this package
Source Files ¶
- context.go
- context_builtin.go
- context_error.go
- group.go
- group_builtin.go
- handler.go
- handler_builtin.go
- introspector.go
- introspector_builtin.go
- introspector_factory.go
- introspector_factory_default.go
- introspector_factory_stdlib.go
- request.go
- request_builtin.go
- response.go
- response_builtin.go
- router.go