mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
new simple _examples/README.md, wiki should live only inside kataras/iris/wiki and the provided e-book
Former-commit-id: 350eafb0f70f8433e394e103ff93fa332ee00a05
This commit is contained in:
34
_examples/dependency-injection/sessions/main.go
Normal file
34
_examples/dependency-injection/sessions/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/_examples/dependency-injection/sessions/routes"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
sessionManager := sessions.New(sessions.Config{
|
||||
Cookie: "site_session_id",
|
||||
Expires: 60 * time.Minute,
|
||||
AllowReclaim: true,
|
||||
})
|
||||
|
||||
// Session is automatically binded through `sessions.Get(ctx)`
|
||||
// if a *sessions.Session input argument is present on the handler's function,
|
||||
// which `routes.Index` does.
|
||||
app.Use(sessionManager.Handler())
|
||||
|
||||
// Method: GET
|
||||
// Path: http://localhost:8080
|
||||
app.ConfigureContainer(registerRoutes)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func registerRoutes(api *iris.APIContainer) {
|
||||
api.Get("/", routes.Index)
|
||||
}
|
||||
17
_examples/dependency-injection/sessions/routes/index.go
Normal file
17
_examples/dependency-injection/sessions/routes/index.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
)
|
||||
|
||||
// Index will increment a simple int version based on the visits that this user/session did.
|
||||
func Index(session *sessions.Session) 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 := session.Increment("visits", 1)
|
||||
|
||||
// write the current, updated visits.
|
||||
return fmt.Sprintf("%d visit(s) from my current session", visits)
|
||||
}
|
||||
Reference in New Issue
Block a user