mvc sessions
Gerasimos (Makis) Maropoulos edited this page Aug 7, 2020
·
1 revision
Pages 72
- What is Iris
📌 Getting Started- Host
- Configuration
- Routing
- HTTP Method Override
- API Versioning
- Content Negotiation
- Response Recorder
- HTTP Referrer
- Request Authentication
- URL Query Parameters
- Forms
- Model Validation
- Cache
- View
- Cookies
- Sessions
- Websockets
- Dependency Injection
- MVC
- gRPC
- Sitemap
- Localization
- Testing
🤓 Resources
Clone this wiki locally
Session Controller
Create a main.go file and copy-paste the following code snippets:
package main
import (
"fmt"
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
"github.com/kataras/iris/v12/sessions"
)
func main() {
app := iris.New()
// Configure sessions manager as we used to.
sess := sessions.New(sessions.Config{Cookie: "mysession_cookie_name"})
app.Use(sess.Handler())
visitApp := mvc.New(app)
visitApp.Register(time.Now())
visitApp.Handle(new(VisitController))
app.Listen(":8080")
}Controller
- The
VisitController.Sessionis automatically binded to the currentsessions.Session. - The
VisitController.StartTimeis statically set to the server's start time with.Register(time.Now())above.
type VisitController struct {
Session *sessions.Session
StartTime time.Time
}
func (c *VisitController) Get() string {
// 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(s) from my current session in %0.1f seconds of server's up-time",
visits, since)
}Run
Open a terminal session and execute:
$ go run main.go- Prepare a client, e.g. your browser
- navigate to http://localhost:8080
- refresh the page some times
- close the browser
- re-open the browser (if it wasn't in private mode) and re-play 2.
Home | Project | Quick Start | Technical Docs | Copyright © 2019-2020 Gerasimos Maropoulos. Documentation terms of use.