mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
Update to 8.3.0 | MVC Models and Bindings and fix of #723 , read HISTORY.md
Former-commit-id: d8f66d8d370c583a288333df2a14c6ee2dc56466
This commit is contained in:
47
_examples/mvc/session-controller/main.go
Normal file
47
_examples/mvc/session-controller/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/mvc"
|
||||
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
type VisitController struct {
|
||||
// if you build with go1.9 you can omit the import of mvc package
|
||||
// and just use `iris.Controller` instead.
|
||||
mvc.SessionController
|
||||
|
||||
StartTime time.Time
|
||||
}
|
||||
|
||||
func (u *VisitController) Get() {
|
||||
// get the visits, before calcuate this new one.
|
||||
visits, _ := u.Session.GetIntDefault("visits", 0)
|
||||
|
||||
// increment the visits counter and set them to the session.
|
||||
visits++
|
||||
u.Session.Set("visits", visits)
|
||||
|
||||
// write the current, updated visits
|
||||
u.Ctx.Writef("%d visits in %0.1f seconds", visits, time.Now().Sub(u.StartTime).Seconds())
|
||||
}
|
||||
|
||||
func main() {
|
||||
mySessionManager := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
|
||||
|
||||
app := iris.New()
|
||||
|
||||
// bind our session manager, which is required, to the `VisitController.SessionManager.Manager`
|
||||
// and the time.Now() to the `VisitController.StartTime`.
|
||||
app.Controller("/", new(VisitController), mySessionManager, time.Now())
|
||||
|
||||
// 1. open the browser (no in private mode)
|
||||
// 2. navigate to http://localhost:8080
|
||||
// 3. refresh the page some times
|
||||
// 4. close the browser
|
||||
// 5. re-open the browser and re-play 2.
|
||||
app.Run(iris.Addr(":8080"), iris.WithoutVersionChecker)
|
||||
}
|
||||
Reference in New Issue
Block a user