mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 11:27:06 +00:00
@@ -6,8 +6,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
"github.com/kataras/iris/v12/sessions"
|
||||
"github.com/kataras/iris/v12/sessions/sessiondb/redis"
|
||||
|
||||
@@ -24,24 +22,18 @@ func main() {
|
||||
Addr: getenv("REDIS_ADDR", "127.0.0.1:6379"),
|
||||
Timeout: time.Duration(30) * time.Second,
|
||||
MaxActive: 10,
|
||||
Username: "",
|
||||
Password: "",
|
||||
Database: "",
|
||||
Prefix: "",
|
||||
Delim: "-",
|
||||
Driver: redis.Redigo(), // redis.Radix() can be used instead.
|
||||
Driver: redis.GoRedis(), // defautls.
|
||||
})
|
||||
|
||||
// Optionally configure the underline driver:
|
||||
// driver := redis.Redigo()
|
||||
// driver.MaxIdle = ...
|
||||
// driver.IdleTimeout = ...
|
||||
// driver.Wait = ...
|
||||
// redis.Config {Driver: driver}
|
||||
|
||||
// Close connection when control+C/cmd+C
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
db.Close()
|
||||
})
|
||||
// driver := redis.GoRedis()
|
||||
// driver.ClientOptions = redis.Options{...}
|
||||
// driver.ClusterOptions = redis.ClusterOptions{...}
|
||||
// redis.New(redis.Config{Driver: driver, ...})
|
||||
|
||||
defer db.Close() // close the database connection if application errored.
|
||||
|
||||
|
||||
@@ -57,12 +57,17 @@ func NewApp(sess *sessions.Sessions) *iris.Application {
|
||||
session := sessions.Get(ctx)
|
||||
session.Set("struct", BusinessModel{Name: "John Doe"})
|
||||
|
||||
ctx.Writef("All ok session value of the 'struct' is: %v", session.Get("struct"))
|
||||
ctx.WriteString("All ok session value of the 'struct' was set.")
|
||||
})
|
||||
|
||||
app.Get("/get-struct", func(ctx iris.Context) {
|
||||
session := sessions.Get(ctx)
|
||||
ctx.Writef("Session value of the 'struct' is: %v", session.Get("struct"))
|
||||
var v BusinessModel
|
||||
if err := session.Decode("struct", &v); err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
ctx.Writef("Session value of the 'struct' is: %#+v", v)
|
||||
})
|
||||
|
||||
app.Get("/set/{key}/{value}", func(ctx iris.Context) {
|
||||
@@ -158,7 +163,12 @@ func NewApp(sess *sessions.Sessions) *iris.Application {
|
||||
|
||||
business := []BusinessModel{{Name: "Edward"}, {Name: "value 2"}}
|
||||
session.SetImmutable("businessEdit", business)
|
||||
businessGet := session.Get("businessEdit").([]BusinessModel)
|
||||
var businessGet []BusinessModel
|
||||
err := session.Decode("businessEdit", &businessGet)
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
// 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.
|
||||
@@ -166,13 +176,19 @@ func NewApp(sess *sessions.Sessions) *iris.Application {
|
||||
})
|
||||
|
||||
app.Get("/get-immutable", func(ctx iris.Context) {
|
||||
valSlice := sessions.Get(ctx).Get("businessEdit")
|
||||
if valSlice == nil {
|
||||
var models []BusinessModel
|
||||
err := sessions.Get(ctx).Decode("businessEdit", &models)
|
||||
if err != nil {
|
||||
ctx.StopWithError(iris.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
if models == nil {
|
||||
ctx.HTML("please navigate to the <a href='/set_immutable'>/set-immutable</a> first")
|
||||
return
|
||||
}
|
||||
|
||||
firstModel := valSlice.([]BusinessModel)[0]
|
||||
firstModel := models[0]
|
||||
// businessGet[0].Name is equal to Edward initially
|
||||
if firstModel.Name != "Edward" {
|
||||
panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable")
|
||||
|
||||
Reference in New Issue
Block a user