1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00:00

add simple tests for _examples/mvc/hello-world and session-controller

Former-commit-id: d88a792ba57cd869d2888f41bca6eb3e5b4f7d49
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-16 21:27:20 +02:00
parent a25c0557de
commit b8cafce6b9
7 changed files with 110 additions and 59 deletions

View File

@@ -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