injekt
Pluggable service injector
Injekt is a pluggable service injector for any project or framework. Injekt adds service injection support by wrapping a custom function (with services as parameters) with the required function.
Usage (http Handler example)
Write custom function. session
will be injected as well as specified parameters of the required function.
func sessionInfo(w http.ResponseWriter, session *Session) {
if session == nil {
// show login page
}
...
}
Wrap custom function. This returns an interface{}
that can be asserted to the required function type.
func main(){
var h http.HandlerFunc
inj := injekt.New(h)
...
http.HandleFunc("/", inj.Wrap(sessionInfo).(http.HandlerFunc))
}
Register services before function is executed.
session := ...
inj.Register(session)
Simple and useful. Check the docs for more.
License
Apache 2