mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
add sharable package-level hero dependencies with the mvc controllers instances
Former-commit-id: 6c53190e40fa42d242a73eb80361830b65f0d077
This commit is contained in:
parent
b4827c2b5e
commit
19e9ed330d
|
@ -21,6 +21,17 @@ func main() {
|
||||||
helloServiceHandler := hero.Handler(helloService)
|
helloServiceHandler := hero.Handler(helloService)
|
||||||
app.Get("/service/{to:string}", helloServiceHandler)
|
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/your_name
|
||||||
// http://localhost:8080/service/your_name
|
// http://localhost:8080/service/your_name
|
||||||
app.Run(iris.Addr(":8080"))
|
app.Run(iris.Addr(":8080"))
|
||||||
|
@ -45,3 +56,12 @@ func (s *myTestService) SayHello(to string) string {
|
||||||
func helloService(to string, service Service) string {
|
func helloService(to string, service Service) string {
|
||||||
return service.SayHello(to)
|
return service.SayHello(to)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LoginForm struct {
|
||||||
|
Username string `form:"username"`
|
||||||
|
Password string `form:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func login(form LoginForm) string {
|
||||||
|
return "Hello " + form.Username
|
||||||
|
}
|
||||||
|
|
26
mvc/mvc.go
26
mvc/mvc.go
|
@ -3,9 +3,26 @@ package mvc
|
||||||
import (
|
import (
|
||||||
"github.com/kataras/iris/context"
|
"github.com/kataras/iris/context"
|
||||||
"github.com/kataras/iris/core/router"
|
"github.com/kataras/iris/core/router"
|
||||||
|
"github.com/kataras/iris/hero"
|
||||||
"github.com/kataras/iris/hero/di"
|
"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.
|
// 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
|
// It's the API that you will be using to register controllers among with their
|
||||||
// dependencies that your controllers may expecting.
|
// 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("/"))`.
|
// Example: `New(app.Party("/todo"))` or `New(app)` as it's the same as `New(app.Party("/"))`.
|
||||||
func New(party router.Party) *Application {
|
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,
|
// 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".
|
// 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".
|
// 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 {
|
func (app *Application) Party(relativePath string, middleware ...context.Handler) *Application {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user