mirror of
https://github.com/kataras/iris.git
synced 2026-01-09 21:15:56 +00:00
minor version 11.2.2 - register sessions as middleware and Context.HTML/Text like Context.Writef
Former-commit-id: 6f5f1c502fb06d739c350c3ecc891f495dc03a6e
This commit is contained in:
@@ -308,7 +308,7 @@ iris cache library lives on its own [package](https://github.com/kataras/iris/tr
|
||||
iris session manager lives on its own [package](https://github.com/kataras/iris/tree/master/sessions).
|
||||
|
||||
- [Overview](sessions/overview/main.go)
|
||||
- [Standalone](sessions/standalone/main.go)
|
||||
- [Middleware](sessions/middleware/main.go)
|
||||
- [Secure Cookie](sessions/securecookie/main.go)
|
||||
- [Flash Messages](sessions/flash-messages/main.go)
|
||||
- [Databases](sessions/database)
|
||||
|
||||
@@ -416,7 +416,7 @@ Iris 独立缓存包 [package](https://github.com/kataras/iris/tree/master/cache
|
||||
Iris session 管理独立包 [package](https://github.com/kataras/iris/tree/master/sessions).
|
||||
|
||||
- [概览](sessions/overview/main.go)
|
||||
- [独立使用](sessions/standalone/main.go)
|
||||
- [中间件](sessions/middleware/main.go)
|
||||
- [安全cookie](sessions/securecookie/main.go)
|
||||
- [临时消息](sessions/flash-messages/main.go)
|
||||
- [数据库](sessions/database)
|
||||
|
||||
@@ -37,11 +37,11 @@ func (ctx *MyContext) Next() {
|
||||
// Override any context's method you want...
|
||||
// [...]
|
||||
|
||||
func (ctx *MyContext) HTML(htmlContents string) (int, error) {
|
||||
func (ctx *MyContext) HTML(format string, args ...interface{}) (int, error) {
|
||||
ctx.Application().Logger().Infof("Executing .HTML function from MyContext")
|
||||
|
||||
ctx.ContentType("text/html")
|
||||
return ctx.WriteString(htmlContents)
|
||||
return ctx.Writef(format, args...)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
@@ -28,6 +28,12 @@ func main() {
|
||||
})
|
||||
|
||||
app.Get("/execute", func(ctx iris.Context) {
|
||||
if !none.IsOnline() {
|
||||
ctx.Values().Set("from", "/execute with offline access")
|
||||
ctx.Exec("NONE", "/invisible/iris")
|
||||
return
|
||||
}
|
||||
|
||||
// same as navigating to "http://localhost:8080/invisible/iris" when /change has being invoked and route state changed
|
||||
// from "offline" to "online"
|
||||
ctx.Values().Set("from", "/execute") // values and session can be shared when calling Exec from a "foreign" context.
|
||||
|
||||
@@ -26,26 +26,33 @@ func main() {
|
||||
// if you want to invalid cookies on different subdomains
|
||||
// of the same host, then enable it.
|
||||
// Defaults to false.
|
||||
DisableSubdomainPersistence: true,
|
||||
// AllowReclaim will allow to
|
||||
// Destroy and Start a session in the same request handler.
|
||||
// All it does is that it removes the cookie for both `Request` and `ResponseWriter` while `Destroy`
|
||||
// or add a new cookie to `Request` while `Start`.
|
||||
//
|
||||
// Defaults to false.
|
||||
AllowReclaim: true,
|
||||
DisableSubdomainPersistence: false,
|
||||
})
|
||||
|
||||
app.Use(sess.Handler()) // session is always non-nil inside handlers now.
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
||||
session := sessions.Get(ctx) // same as sess.Start(ctx, cookieOptions...)
|
||||
if session.Len() == 0 {
|
||||
ctx.HTML(`no session values stored yet. Navigate to: <a href="/set">set page</a>`)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.HTML("<ul>")
|
||||
session.Visit(func(key string, value interface{}) {
|
||||
ctx.HTML("<li> %s = %v </li>", key, value)
|
||||
})
|
||||
|
||||
ctx.HTML("</ul>")
|
||||
})
|
||||
|
||||
//set session values.
|
||||
app.Get("/set", func(ctx iris.Context) {
|
||||
//set session values.
|
||||
s := sess.Start(ctx)
|
||||
s.Set("name", "iris")
|
||||
session := sessions.Get(ctx)
|
||||
session.Set("name", "iris")
|
||||
|
||||
//test if set here.
|
||||
ctx.Writef("All ok session set to: %s", s.GetString("name"))
|
||||
ctx.Writef("All ok session set to: %s", session.GetString("name"))
|
||||
|
||||
// Set will set the value as-it-is,
|
||||
// if it's a slice or map
|
||||
@@ -56,22 +63,32 @@ func main() {
|
||||
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
key, value := ctx.Params().Get("key"), ctx.Params().Get("value")
|
||||
|
||||
session := sessions.Get(ctx)
|
||||
session.Set(key, value)
|
||||
|
||||
// test if set here
|
||||
ctx.Writef("All ok session value of the '%s' is: %s", key, session.GetString(key))
|
||||
})
|
||||
|
||||
app.Get("/get", func(ctx iris.Context) {
|
||||
// get a specific value, as string,
|
||||
// if not found then it returns just an empty string.
|
||||
name := sess.Start(ctx).GetString("name")
|
||||
name := sessions.Get(ctx).GetString("name")
|
||||
|
||||
ctx.Writef("The name on the /set was: %s", name)
|
||||
})
|
||||
|
||||
app.Get("/delete", func(ctx iris.Context) {
|
||||
// delete a specific key
|
||||
sess.Start(ctx).Delete("name")
|
||||
sessions.Get(ctx).Delete("name")
|
||||
})
|
||||
|
||||
app.Get("/clear", func(ctx iris.Context) {
|
||||
// removes all entries.
|
||||
sess.Start(ctx).Clear()
|
||||
sessions.Get(ctx).Clear()
|
||||
})
|
||||
|
||||
app.Get("/update", func(ctx iris.Context) {
|
||||
@@ -81,13 +98,15 @@ func main() {
|
||||
|
||||
app.Get("/destroy", func(ctx iris.Context) {
|
||||
//destroy, removes the entire session data and cookie
|
||||
sess.Destroy(ctx)
|
||||
// sess.Destroy(ctx)
|
||||
// or
|
||||
sessions.Get(ctx).Destroy()
|
||||
})
|
||||
// Note about Destroy:
|
||||
//
|
||||
// You can destroy a session outside of a handler too, using the:
|
||||
// mySessions.DestroyByID
|
||||
// mySessions.DestroyAll
|
||||
// sess.DestroyByID
|
||||
// sess.DestroyAll
|
||||
|
||||
// remember: slices and maps are muttable by-design
|
||||
// The `SetImmutable` makes sure that they will be stored and received
|
||||
@@ -97,9 +116,9 @@ func main() {
|
||||
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
||||
app.Get("/set_immutable", func(ctx iris.Context) {
|
||||
business := []businessModel{{Name: "Edward"}, {Name: "value 2"}}
|
||||
s := sess.Start(ctx)
|
||||
s.SetImmutable("businessEdit", business)
|
||||
businessGet := s.Get("businessEdit").([]businessModel)
|
||||
session := sessions.Get(ctx)
|
||||
session.SetImmutable("businessEdit", business)
|
||||
businessGet := session.Get("businessEdit").([]businessModel)
|
||||
|
||||
// try to change it, if we used `Set` instead of `SetImmutable` this
|
||||
// change will affect the underline array of the session's value "businessEdit", but now it will not.
|
||||
@@ -108,7 +127,7 @@ func main() {
|
||||
})
|
||||
|
||||
app.Get("/get_immutable", func(ctx iris.Context) {
|
||||
valSlice := sess.Start(ctx).Get("businessEdit")
|
||||
valSlice := sessions.Get(ctx).Get("businessEdit")
|
||||
if valSlice == nil {
|
||||
ctx.HTML("please navigate to the <a href='/set_immutable'>/set_immutable</a> first")
|
||||
return
|
||||
Reference in New Issue
Block a user