diff --git a/_examples/mvc/hello-world/main.go b/_examples/mvc/hello-world/main.go index 006c818c..ca8550be 100644 --- a/_examples/mvc/hello-world/main.go +++ b/_examples/mvc/hello-world/main.go @@ -28,7 +28,9 @@ import ( // what suits you best with Iris, low-level handlers: performance // or high-level controllers: easier to maintain and smaller codebase on large applications. -func main() { +// Of course you can put all these to main func, it's just a separate function +// for the main_test.go. +func newApp() *iris.Application { app := iris.New() // Optionally, add two built'n handlers // that can recover from any http-relative panics @@ -38,6 +40,11 @@ func main() { // Register a controller based on the root Router, "/". mvc.New(app).Register(new(ExampleController)) + return app +} + +func main() { + app := newApp() // http://localhost:8080 // http://localhost:8080/ping diff --git a/_examples/mvc/hello-world/main_test.go b/_examples/mvc/hello-world/main_test.go new file mode 100644 index 00000000..5c8e2719 --- /dev/null +++ b/_examples/mvc/hello-world/main_test.go @@ -0,0 +1,23 @@ +package main + +import ( + "testing" + + "github.com/kataras/iris/httptest" +) + +func TestMVCHelloWorld(t *testing.T) { + e := httptest.New(t, newApp()) + + e.GET("/").Expect().Status(httptest.StatusOK). + ContentType("text/html", "utf-8").Body().Equal("

Welcome

") + + e.GET("/ping").Expect().Status(httptest.StatusOK). + Body().Equal("pong") + + e.GET("/hello").Expect().Status(httptest.StatusOK). + JSON().Object().Value("message").Equal("Hello Iris!") + + e.GET("/custom_path").Expect().Status(httptest.StatusOK). + Body().Equal("hello from the custom handler without following the naming guide") +} diff --git a/_examples/mvc/session-controller/main.go b/_examples/mvc/session-controller/main.go index b768da68..19281993 100644 --- a/_examples/mvc/session-controller/main.go +++ b/_examples/mvc/session-controller/main.go @@ -7,18 +7,14 @@ import ( "time" "github.com/kataras/iris" + "github.com/kataras/iris/mvc" "github.com/kataras/iris/sessions" ) // VisitController handles the root route. type VisitController struct { - iris.C - - // the sessions manager, we need that to set `Session`. - // It's binded from `app.Controller`. - Manager *sessions.Sessions // the current request session, - // its initialization happens at the `BeginRequest`. + // its initialization happens by the dependency function that we've added to the `visitApp`. Session *sessions.Session // A time.time which is binded from the `app.Controller`, @@ -26,53 +22,49 @@ type VisitController struct { StartTime time.Time } -// BeginRequest is executed for each Get, Post, Put requests, -// can be used to share context, common data -// or to cancel the request via `ctx.StopExecution()`. -func (c *VisitController) BeginRequest(ctx iris.Context) { - // always call the embedded `BeginRequest` before everything else. - c.C.BeginRequest(ctx) - - if c.Manager == nil { - ctx.Application().Logger().Errorf(`VisitController: sessions manager is nil, you should bind it`) - // dont run the main method handler and any "done" handlers. - ctx.StopExecution() - return - } - - // set the `c.Session` we will use that in our Get method. - c.Session = c.Manager.Start(ctx) -} - // Get handles // Method: GET // Path: http://localhost:8080 func (c *VisitController) Get() string { - // get the visits, before calcuate this new one. - visits, _ := c.Session.GetIntDefault("visits", 0) - - // increment the visits and store to the session. - visits++ - c.Session.Set("visits", visits) - + // it increments a "visits" value of integer by one, + // if the entry with key 'visits' doesn't exist it will create it for you. + visits := c.Session.Increment("visits", 1) // write the current, updated visits. since := time.Now().Sub(c.StartTime).Seconds() return fmt.Sprintf("%d visit from my current session in %0.1f seconds of server's up-time", visits, since) } -var ( - manager = sessions.New(sessions.Config{Cookie: "mysession_cookie_name"}) -) +func newApp() *iris.Application { + app := iris.New() + sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"}) + + visitApp := mvc.New(app.Party("/")) + // bind the current *session.Session, which is required, to the `VisitController.Session` + // and the time.Now() to the `VisitController.StartTime`. + visitApp.AddDependencies( + // if dependency is a function which accepts + // a Context and returns a single value + // then the result type of this function is resolved by the controller + // and on each request it will call the function with its Context + // and set the result(the *sessions.Session here) to the controller's field. + // + // If dependencies are registered without field or function's input arguments as + // consumers then those dependencies are being ignored before the server ran, + // so you can bind many dependecies and use them in different controllers. + // func(ctx iris.Context) *sessions.Session { + // return sess.Start(ctx) + // }, -> same as mvc.Session(sess): + mvc.Session(sess), + time.Now(), + ) + visitApp.Register(new(VisitController)) + + return app +} func main() { - app := iris.New() - - // bind our session manager, which is required, to the `VisitController.Manager` - // and the time.Now() to the `VisitController.StartTime`. - app.Controller("/", new(VisitController), - manager, - time.Now()) + app := newApp() // 1. open the browser (no in private mode) // 2. navigate to http://localhost:8080 diff --git a/_examples/mvc/session-controller/main_test.go b/_examples/mvc/session-controller/main_test.go new file mode 100644 index 00000000..f8900501 --- /dev/null +++ b/_examples/mvc/session-controller/main_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "testing" + + "github.com/kataras/iris/httptest" +) + +func TestMVCSession(t *testing.T) { + e := httptest.New(t, newApp(), httptest.URL("http://example.com")) + + e1 := e.GET("/").Expect().Status(httptest.StatusOK) + e1.Cookies().NotEmpty() + e1.Body().Contains("1 visit") + + e.GET("/").Expect().Status(httptest.StatusOK). + Body().Contains("2 visit") + + e.GET("/").Expect().Status(httptest.StatusOK). + Body().Contains("3 visit") +} diff --git a/core/router/handler.go b/core/router/handler.go index 0114f9da..bb00eb3a 100644 --- a/core/router/handler.go +++ b/core/router/handler.go @@ -1,14 +1,14 @@ package router import ( - "github.com/kataras/golog" "html" "net/http" "sort" "strings" - "github.com/kataras/iris/context" + "github.com/kataras/golog" + "github.com/kataras/iris/context" "github.com/kataras/iris/core/errors" "github.com/kataras/iris/core/netutil" "github.com/kataras/iris/core/router/node" diff --git a/mvc/controller.go b/mvc/controller.go index 57d54dac..33678313 100644 --- a/mvc/controller.go +++ b/mvc/controller.go @@ -58,8 +58,6 @@ func getNameOf(typ reflect.Type) string { return fullname } -/// TODO: activate controllers with go routines so the startup time of iris -// can be improved on huge applications. func newControllerActivator(router router.Party, controller interface{}, d *di.D) *ControllerActivator { var ( val = reflect.ValueOf(controller) @@ -121,24 +119,30 @@ func (c *ControllerActivator) isReservedMethod(name string) bool { return false } +func (c *ControllerActivator) parseMethod(m reflect.Method) { + httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod) + if err != nil { + if err != errSkip { + err = fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.FullName, m.Name, err) + c.Router.GetReporter().AddErr(err) + + } + return + } + + c.Handle(httpMethod, httpPath, m.Name) +} + // register all available, exported methods to handlers if possible. func (c *ControllerActivator) parseMethods() { n := c.Type.NumMethod() + // wg := &sync.WaitGroup{} + // wg.Add(n) for i := 0; i < n; i++ { m := c.Type.Method(i) - - httpMethod, httpPath, err := parseMethod(m, c.isReservedMethod) - if err != nil { - if err != errSkip { - err = fmt.Errorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.FullName, m.Name, err) - c.Router.GetReporter().AddErr(err) - - } - continue - } - - c.Handle(httpMethod, httpPath, m.Name) + c.parseMethod(m) } + // wg.Wait() } func (c *ControllerActivator) activate() { @@ -248,7 +252,7 @@ func buildHandler(m reflect.Method, typ reflect.Type, initRef reflect.Value, str elemTyp = di.IndirectType(typ) ) - // if it doesn't implements the base controller, + // if it doesn't implement the base controller, // it may have struct injector and/or func injector. if !implementsBase { diff --git a/mvc/ideas/1/main.go b/mvc/ideas/1/main.go index 1de61ec9..88c120d1 100644 --- a/mvc/ideas/1/main.go +++ b/mvc/ideas/1/main.go @@ -9,6 +9,10 @@ import ( "github.com/kataras/iris/mvc" ) +// TODO: It's not here but this file is what I'll see before the commit in order to delete it: +// Think a way to simplify the router cycle, I did create it to support any type of router +// but as I see nobody wants to override the iris router's behavior(I'm not speaking about wrapper, this will stay of course because it's useful on security-critical middlewares) because it's the best by far. +// Therefore I should reduce some "freedom of change" for the shake of code maintanability in the core/router files: handler.go | router.go and single change on APIBuilder's field. func main() { app := iris.New() mvc.New(app.Party("/todo")).Configure(TodoApp)