mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 12:31:58 +00:00
add example for hero *sessions.Session dependency which is used on an index route outside of the main package, it is as easy as it shown. Added mostly after the issue: https://github.com/kataras/iris/issues/1057 -- have fun
Former-commit-id: 93338d0e03d6be885edf783c09a2c181568e9ec5
This commit is contained in:
40
_examples/hero/sessions/main.go
Normal file
40
_examples/hero/sessions/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/_examples/hero/sessions/routes"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/hero" // <- IMPORTANT
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
sessionManager := sessions.New(sessions.Config{
|
||||
Cookie: "site_session_id",
|
||||
Expires: 60 * time.Minute,
|
||||
AllowReclaim: true,
|
||||
})
|
||||
|
||||
// Register
|
||||
// dynamic dependencies like the *sessions.Session, from `sessionManager.Start(ctx) *sessions.Session` <- it accepts a Context and it returns
|
||||
// something -> this is called dynamic request-time dependency and that "something" can be used to your handlers as input parameters,
|
||||
// no limit about the number of dependencies, each handler will be builded once, before the server ran and it will use only dependencies that it needs.
|
||||
hero.Register(sessionManager.Start)
|
||||
// convert any function to an iris Handler, their input parameters are being resolved using the unique Iris' blazing-fast dependency injection
|
||||
// for services or dynamic dependencies like the *sessions.Session, from sessionManager.Start(ctx) *sessions.Session) <- it accepts a context and it returns
|
||||
// something-> this is called dynamic request-time dependency.
|
||||
indexHandler := hero.Handler(routes.Index)
|
||||
|
||||
// Method: GET
|
||||
// Path: http://localhost:8080
|
||||
app.Get("/", indexHandler)
|
||||
|
||||
app.Run(
|
||||
iris.Addr(":8080"),
|
||||
iris.WithoutVersionChecker,
|
||||
iris.WithoutServerError(iris.ErrServerClosed),
|
||||
)
|
||||
}
|
||||
26
_examples/hero/sessions/routes/index.go
Normal file
26
_examples/hero/sessions/routes/index.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
)
|
||||
|
||||
// Index will increment a simple int version based on the visits that this user/session did.
|
||||
func Index(ctx iris.Context, session *sessions.Session) {
|
||||
// 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.
|
||||
ctx.Writef("%d visit(s) from my current session", visits)
|
||||
}
|
||||
|
||||
/*
|
||||
You can also do anything that an MVC function can, i.e:
|
||||
|
||||
func Index(ctx iris.Context,session *sessions.Session) string {
|
||||
visits := session.Increment("visits", 1)
|
||||
return fmt.Spritnf("%d visit(s) from my current session", visits)
|
||||
}
|
||||
// you can also omit iris.Context input parameter and use dependency injection for LoginForm and etc. <- look the mvc examples.
|
||||
*/
|
||||
Reference in New Issue
Block a user