mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 09:57:01 +00:00
Replace controller's .Register with .Handle and AddDependencies with .Register in order to be aligned with the 'hero' package, all examples and docs are updated, it's crazy how I can't stop even on Christmas
Former-commit-id: 3b42963e9806e327ee42942cf156bda6059eaf8f
This commit is contained in:
@@ -71,8 +71,8 @@ func TestControllerHandle(t *testing.T) {
|
||||
app := iris.New()
|
||||
|
||||
m := New(app)
|
||||
m.AddDependencies(&TestServiceImpl{prefix: "service:"})
|
||||
m.Register(new(testControllerHandle))
|
||||
m.Register(&TestServiceImpl{prefix: "service:"})
|
||||
m.Handle(new(testControllerHandle))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ func (c *testControllerMethodResult) GetThingWithTryDefaultBy(index int) Result
|
||||
|
||||
func TestControllerMethodResult(t *testing.T) {
|
||||
app := iris.New()
|
||||
New(app).Register(new(testControllerMethodResult))
|
||||
New(app).Handle(new(testControllerMethodResult))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
@@ -173,7 +173,7 @@ func (c *testControllerMethodResultTypes) GetCustomStructWithError() (s testCust
|
||||
|
||||
func TestControllerMethodResultTypes(t *testing.T) {
|
||||
app := iris.New()
|
||||
New(app).Register(new(testControllerMethodResultTypes))
|
||||
New(app).Handle(new(testControllerMethodResultTypes))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
@@ -262,8 +262,8 @@ func (t *testControllerViewResultRespectCtxViewData) Get() Result {
|
||||
func TestControllerViewResultRespectCtxViewData(t *testing.T) {
|
||||
app := iris.New()
|
||||
m := New(app.Party("/"))
|
||||
m.AddDependencies(t)
|
||||
m.Register(new(testControllerViewResultRespectCtxViewData))
|
||||
m.Register(t)
|
||||
m.Handle(new(testControllerViewResultRespectCtxViewData))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
|
||||
|
||||
@@ -64,9 +64,9 @@ func (c *testControllerAny) Any() {
|
||||
func TestControllerMethodFuncs(t *testing.T) {
|
||||
app := iris.New()
|
||||
|
||||
New(app).Register(new(testController))
|
||||
New(app.Party("/all")).Register(new(testControllerAll))
|
||||
New(app.Party("/any")).Register(new(testControllerAny))
|
||||
New(app).Handle(new(testController))
|
||||
New(app.Party("/all")).Handle(new(testControllerAll))
|
||||
New(app.Party("/any")).Handle(new(testControllerAny))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
for _, method := range router.AllMethods {
|
||||
@@ -112,7 +112,7 @@ func (c *testControllerBeginAndEndRequestFunc) Post() {
|
||||
func TestControllerBeginAndEndRequestFunc(t *testing.T) {
|
||||
app := iris.New()
|
||||
New(app.Party("/profile/{username}")).
|
||||
Register(new(testControllerBeginAndEndRequestFunc))
|
||||
Handle(new(testControllerBeginAndEndRequestFunc))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
usernames := []string{
|
||||
@@ -157,7 +157,7 @@ func TestControllerBeginAndEndRequestFuncBindMiddleware(t *testing.T) {
|
||||
|
||||
app.PartyFunc("/profile/{username}", func(r iris.Party) {
|
||||
r.Use(middlewareCheck)
|
||||
New(r).Register(new(testControllerBeginAndEndRequestFunc))
|
||||
New(r).Handle(new(testControllerBeginAndEndRequestFunc))
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
@@ -231,7 +231,7 @@ func (c *testControllerEndRequestAwareness) EndRequest(ctx context.Context) {
|
||||
|
||||
func TestControllerEndRequestAwareness(t *testing.T) {
|
||||
app := iris.New()
|
||||
New(app.Party("/era/{username}")).Register(new(testControllerEndRequestAwareness))
|
||||
New(app.Party("/era/{username}")).Handle(new(testControllerEndRequestAwareness))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
usernames := []string{
|
||||
@@ -287,9 +287,9 @@ func TestControllerDependencies(t *testing.T) {
|
||||
// test bind value to value of the correct type
|
||||
myTitleV := testBindType{title: t2}
|
||||
m := New(app)
|
||||
m.AddDependencies(myTitlePtr, myTitleV)
|
||||
m.Register(new(testControllerBindStruct))
|
||||
m.NewChild(app.Party("/deep")).Register(new(testControllerBindDeep))
|
||||
m.Register(myTitlePtr, myTitleV)
|
||||
m.Handle(new(testControllerBindStruct))
|
||||
m.Clone(app.Party("/deep")).Handle(new(testControllerBindDeep))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
expected := t1 + t2
|
||||
@@ -349,8 +349,8 @@ func TestControllerInsideControllerRecursively(t *testing.T) {
|
||||
|
||||
app := iris.New()
|
||||
m := New(app.Party("/user/{username}"))
|
||||
m.AddDependencies(&testBindType{title: title})
|
||||
m.Register(new(testCtrl0))
|
||||
m.Register(&testBindType{title: title})
|
||||
m.Handle(new(testCtrl0))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/user/" + username).Expect().
|
||||
@@ -382,7 +382,7 @@ func (c *testControllerRelPathFromFunc) GetSomethingByElseThisBy(bool, int) {} /
|
||||
|
||||
func TestControllerRelPathFromFunc(t *testing.T) {
|
||||
app := iris.New()
|
||||
New(app).Register(new(testControllerRelPathFromFunc))
|
||||
New(app).Handle(new(testControllerRelPathFromFunc))
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusOK).
|
||||
@@ -432,14 +432,14 @@ func (c *testControllerActivateListener) Get() string {
|
||||
|
||||
func TestControllerActivateListener(t *testing.T) {
|
||||
app := iris.New()
|
||||
New(app).Register(new(testControllerActivateListener))
|
||||
New(app).Handle(new(testControllerActivateListener))
|
||||
m := New(app)
|
||||
m.AddDependencies(&testBindType{
|
||||
m.Register(&testBindType{
|
||||
title: "my title",
|
||||
})
|
||||
m.NewChild(m.Router.Party("/manual")).Register(new(testControllerActivateListener))
|
||||
m.Party("/manual").Handle(new(testControllerActivateListener))
|
||||
// or
|
||||
m.NewChild(m.Router.Party("/manual2")).Register(&testControllerActivateListener{
|
||||
m.Party("/manual2").Handle(&testControllerActivateListener{
|
||||
TitlePointer: &testBindType{
|
||||
title: "my title",
|
||||
},
|
||||
|
||||
@@ -16,11 +16,7 @@ import (
|
||||
func main() {
|
||||
app := iris.New()
|
||||
app.Logger().SetLevel("debug")
|
||||
mvc.New(app.Party("/todo")).Configure(TodoApp)
|
||||
// no let's have a clear "mvc" package without any conversions and type aliases,
|
||||
// it's one extra import path for a whole new world, it worths it.
|
||||
//
|
||||
// app.UseMVC(app.Party("/todo")).Configure(func(app *iris.MVCApplication))
|
||||
mvc.Configure(app.Party("/todo"), TodoApp)
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -32,20 +28,20 @@ func TodoApp(app *mvc.Application) {
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
// Add dependencies which will be binding to the controller(s),
|
||||
// Register dependencies which will be binding to the controller(s),
|
||||
// can be either a function which accepts an iris.Context and returns a single value (dynamic binding)
|
||||
// or a static struct value (service).
|
||||
app.AddDependencies(
|
||||
app.Register(
|
||||
sessions.New(sessions.Config{}).Start,
|
||||
&prefixedLogger{prefix: "DEV"},
|
||||
)
|
||||
|
||||
app.Register(new(TodoController))
|
||||
app.Handle(new(TodoController))
|
||||
|
||||
// All dependencies of the parent *mvc.Application
|
||||
// are cloned to that new child, thefore it has access to the same session as well.
|
||||
app.NewChild(app.Router.Party("/sub")).
|
||||
Register(new(TodoSubController))
|
||||
app.Party("/sub").
|
||||
Handle(new(TodoSubController))
|
||||
}
|
||||
|
||||
// If controller's fields (or even its functions) expecting an interface
|
||||
|
||||
33
mvc/mvc.go
33
mvc/mvc.go
@@ -1,6 +1,7 @@
|
||||
package mvc
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/core/router"
|
||||
"github.com/kataras/iris/hero/di"
|
||||
)
|
||||
@@ -68,7 +69,7 @@ func (app *Application) Configure(configurators ...func(*Application)) *Applicat
|
||||
return app
|
||||
}
|
||||
|
||||
// AddDependencies adds one or more values as dependencies.
|
||||
// Register appends one or more values as dependencies.
|
||||
// The value can be a single struct value-instance or a function
|
||||
// which has one input and one output, the input should be
|
||||
// an `iris.Context` and the output can be any type, that output type
|
||||
@@ -77,17 +78,17 @@ func (app *Application) Configure(configurators ...func(*Application)) *Applicat
|
||||
//
|
||||
// These dependencies "values" can be changed per-controller as well,
|
||||
// via controller's `BeforeActivation` and `AfterActivation` methods,
|
||||
// look the `Register` method for more.
|
||||
// look the `Handle` method for more.
|
||||
//
|
||||
// It returns this Application.
|
||||
//
|
||||
// Example: `.AddDependencies(loggerService{prefix: "dev"}, func(ctx iris.Context) User {...})`.
|
||||
func (app *Application) AddDependencies(values ...interface{}) *Application {
|
||||
// Example: `.Register(loggerService{prefix: "dev"}, func(ctx iris.Context) User {...})`.
|
||||
func (app *Application) Register(values ...interface{}) *Application {
|
||||
app.Dependencies.Add(values...)
|
||||
return app
|
||||
}
|
||||
|
||||
// Register adds a controller for the current Router.
|
||||
// Handle serves a controller for the current mvc application's Router.
|
||||
// It accept any custom struct which its functions will be transformed
|
||||
// to routes.
|
||||
//
|
||||
@@ -98,7 +99,7 @@ func (app *Application) AddDependencies(values ...interface{}) *Application {
|
||||
//
|
||||
// It returns this mvc Application.
|
||||
//
|
||||
// Usage: `.Register(new(TodoController))`.
|
||||
// Usage: `.Handle(new(TodoController))`.
|
||||
//
|
||||
// Controller accepts a sub router and registers any custom struct
|
||||
// as controller, if struct doesn't have any compatible methods
|
||||
@@ -131,7 +132,7 @@ func (app *Application) AddDependencies(values ...interface{}) *Application {
|
||||
// where Get is an HTTP Method func.
|
||||
//
|
||||
// Examples at: https://github.com/kataras/iris/tree/master/_examples/mvc
|
||||
func (app *Application) Register(controller interface{}) *Application {
|
||||
func (app *Application) Handle(controller interface{}) *Application {
|
||||
// initialize the controller's activator, nothing too magical so far.
|
||||
c := newControllerActivator(app.Router, controller, app.Dependencies)
|
||||
|
||||
@@ -154,12 +155,18 @@ func (app *Application) Register(controller interface{}) *Application {
|
||||
return app
|
||||
}
|
||||
|
||||
// NewChild creates and returns a new MVC Application which will be adapted
|
||||
// to the "party", it adopts
|
||||
// the parent's (current) dependencies, the "party" may be
|
||||
// a totally new router or a child path one via the parent's `.Router.Party`.
|
||||
// Clone returns a new mvc Application which has the dependencies
|
||||
// of the current mvc Mpplication's dependencies.
|
||||
//
|
||||
// Example: `.NewChild(irisApp.Party("/path")).Register(new(TodoSubController))`.
|
||||
func (app *Application) NewChild(party router.Party) *Application {
|
||||
// Example: `.Clone(app.Party("/path")).Handle(new(TodoSubController))`.
|
||||
func (app *Application) Clone(party router.Party) *Application {
|
||||
return newApp(party, app.Dependencies.Clone())
|
||||
}
|
||||
|
||||
// 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 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 {
|
||||
return app.Clone(app.Router.Party(relativePath, middleware...))
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ var defaultSessionManager = sessions.New(sessions.Config{})
|
||||
// direct access to the current client's session via its `Session` field.
|
||||
//
|
||||
// SessionController is deprecated please use the new dependency injection's methods instead,
|
||||
// i.e `mvcApp.AddDependencies(sessions.New(sessions.Config{}).Start)`.
|
||||
// i.e `mvcApp.Register(sessions.New(sessions.Config{}).Start)`.
|
||||
// It's more controlled by you,
|
||||
// also *sessions.Session type can now `Destroy` itself without the need of the manager, embrace it.
|
||||
type SessionController struct {
|
||||
@@ -23,7 +23,7 @@ type SessionController struct {
|
||||
// BeforeActivation called, once per application lifecycle NOT request,
|
||||
// every single time the dev registers a specific SessionController-based controller.
|
||||
// It makes sure that its "Manager" field is filled
|
||||
// even if the caller didn't provide any sessions manager via the MVC's Application's `Register` function.
|
||||
// even if the caller didn't provide any sessions manager via the MVC's Application's `Handle` function.
|
||||
func (s *SessionController) BeforeActivation(b BeforeActivation) {
|
||||
if didntBindManually := b.Dependencies().AddOnce(defaultSessionManager); didntBindManually {
|
||||
b.Router().GetReporter().Add(
|
||||
|
||||
Reference in New Issue
Block a user