From 19e9ed330dd21a18cc84ef308538e982c17956d8 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 27 Dec 2017 15:09:52 +0200 Subject: [PATCH] add sharable package-level hero dependencies with the mvc controllers instances Former-commit-id: 6c53190e40fa42d242a73eb80361830b65f0d077 --- _examples/hero/basic/main.go | 20 ++++++++++++++++++++ mvc/mvc.go | 26 ++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/_examples/hero/basic/main.go b/_examples/hero/basic/main.go index 80d481f3..90015ed3 100644 --- a/_examples/hero/basic/main.go +++ b/_examples/hero/basic/main.go @@ -21,6 +21,17 @@ func main() { helloServiceHandler := hero.Handler(helloService) app.Get("/service/{to:string}", helloServiceHandler) + // 3 + hero.Register(func(ctx iris.Context) (form LoginForm) { + // it binds the "form" with a + // x-www-form-urlencoded form data and returns it. + ctx.ReadForm(&form) + return + }) + + loginHandler := hero.Handler(login) + app.Post("/login", loginHandler) + // http://localhost:8080/your_name // http://localhost:8080/service/your_name app.Run(iris.Addr(":8080")) @@ -45,3 +56,12 @@ func (s *myTestService) SayHello(to string) string { func helloService(to string, service Service) string { return service.SayHello(to) } + +type LoginForm struct { + Username string `form:"username"` + Password string `form:"password"` +} + +func login(form LoginForm) string { + return "Hello " + form.Username +} diff --git a/mvc/mvc.go b/mvc/mvc.go index e1445dae..f11e02d4 100644 --- a/mvc/mvc.go +++ b/mvc/mvc.go @@ -3,9 +3,26 @@ package mvc import ( "github.com/kataras/iris/context" "github.com/kataras/iris/core/router" + "github.com/kataras/iris/hero" "github.com/kataras/iris/hero/di" ) +var ( + // HeroDependencies let you share bindable dependencies between + // package-level hero's registered dependencies and all MVC instances that comes later. + // + // `hero.Register(...)` + // `myMVC := mvc.New(app.Party(...))` + // the "myMVC" registers the dependencies provided by the `hero.Register` func + // automatically. + // + // Set it to false to disable that behavior, you have to use the `mvc#Register` + // even if you had register dependencies with the `hero` package. + // + // Defaults to true. + HeroDependencies = true +) + // Application is the high-level compoment of the "mvc" package. // It's the API that you will be using to register controllers among with their // dependencies that your controllers may expecting. @@ -35,7 +52,11 @@ func newApp(subRouter router.Party, values di.Values) *Application { // // Example: `New(app.Party("/todo"))` or `New(app)` as it's the same as `New(app.Party("/"))`. func New(party router.Party) *Application { - return newApp(party, di.NewValues()) + values := di.NewValues() + if HeroDependencies { + values = hero.Dependencies().Clone() + } + return newApp(party, values) } // Configure creates a new controller and configures it, @@ -164,7 +185,8 @@ func (app *Application) Clone(party router.Party) *Application { } // Party returns a new child mvc Application based on the current path + "relativePath". -// The new mvc Application has the same dependencies of the current mvc Application. +// The new mvc Application has the same dependencies of the current mvc Application, +// until otherwise specified later manually. // // The router's root path of this child will be the current mvc Application's root path + "relativePath". func (app *Application) Party(relativePath string, middleware ...context.Handler) *Application {