Overview
Go-Mojito allows you to mix and match your handler arguments, so you can choose only what you need. Arguments can either be built-in and/or custom request arguments. If they are neither, Go-Mojito will attempt to dependency-inject them.
This is a core part of the system, and works the same across all router implementations. This also means that this part cannot be swapped out easily.
Quick Start
-
Create a handler func without any parameters
func myHandler() {}
-
Add
mojito.Context
,*http.Request
andmojito.Cache
arguments to the handler.func myHandler(ctx mojito.Context, r *http.Request, cache mojito.Cache) {}
Go-Mojito will now detect that you want a
mojito.Context
, a standard lib*http.Request
and the default dependency formojito.Cache
injected into your handler.
FAQ
To inject a non-default instance of a dependency, you need to create a context struct which contains the named dependencies you want to use in your handler. By using the `container:"name"`
tag, you indicate that you want to resolve by the field's name and not solely by the type.
type HandlerContext struct {
CacheName mojito.Cache `container:"name"`
}
func SomeHandler(ctx mojito.Context, handlerCtx *HandlerContext) {}
In the example above, Go-Mojito will try to link the named dependency "CacheName" of the type mojito.Cache. This will fail if there is no such named dependency available.