iris/_examples/mvc/session-controller/main.go
Gerasimos (Makis) Maropoulos e5600fcf0d Update to version 8.4.5 | Read https://github.com/kataras/iris/blob/master/HISTORY.md#fr-06-october-2017--v845 for more.
Former-commit-id: 4e30bf6a74acb7d463c7fcf1e9e08ca3147117a7
2017-10-06 08:23:03 +03:00

46 lines
1.2 KiB
Go

package main
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/sessions"
)
type VisitController struct {
iris.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 and store to the session.
visits++
u.Session.Set("visits", visits)
// write the current, updated visits
u.Ctx.Writef("%d visit from my current session in %0.1f seconds of server's up-time",
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)
}