forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
203 lines (162 loc) · 5.68 KB
/
example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package example
import (
"errors"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/sessions"
)
// BusinessModel is just a Go struct value that we will use in our session example,
// never save sensitive information, like passwords, here.
type BusinessModel struct {
Name string
}
// NewApp returns a new application for showcasing the sessions feature.
func NewApp(sess *sessions.Sessions) *iris.Application {
app := iris.New()
app.Use(sess.Handler()) // register the session manager on a group of routes or the root app.
app.Get("/", func(ctx iris.Context) {
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) {
session := sessions.Get(ctx)
isNew := session.IsNew()
session.Set("username", "iris")
ctx.Writef("All ok session set to: %s [isNew=%t]", session.GetString("username"), isNew)
})
app.Get("/get", func(ctx iris.Context) {
session := sessions.Get(ctx)
// get a specific value, as string,
// if not found then it returns just an empty string.
name := session.GetString("username")
ctx.Writef("The username on the /set was: %s", name)
})
app.Get("/set-struct", func(ctx iris.Context) {
session := sessions.Get(ctx)
session.Set("struct", BusinessModel{Name: "John Doe"})
ctx.WriteString("All ok session value of the 'struct' was set.")
})
app.Get("/get-struct", func(ctx iris.Context) {
session := sessions.Get(ctx)
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) {
session := sessions.Get(ctx)
key := ctx.Params().Get("key")
value := ctx.Params().Get("value")
isNew := session.IsNew()
session.Set(key, value)
ctx.Writef("All ok session value of the '%s' is: %s [isNew=%t]", key, session.GetString(key), isNew)
})
app.Get("/get/{key}", func(ctx iris.Context) {
session := sessions.Get(ctx)
// get a specific key, as string, if no found returns just an empty string
key := ctx.Params().Get("key")
value := session.Get(key)
ctx.Writef("The [%s:%T] on the /set was: %v", key, value, value)
})
app.Get("/set/{type}/{key}/{value}", func(ctx iris.Context) {
session := sessions.Get(ctx)
key := ctx.Params().Get("key")
var value interface{}
switch ctx.Params().Get("type") {
case "int":
value = ctx.Params().GetIntDefault("value", 0)
case "float64":
value = ctx.Params().GetFloat64Default("value", 0.0)
default:
value = ctx.Params().Get("value")
}
session.Set(key, value)
value = session.Get(key)
ctx.Writef("Key: %s, Type: %T, Value: %v", key, value, value)
})
app.Get("/delete", func(ctx iris.Context) {
session := sessions.Get(ctx)
// delete a specific key
session.Delete("username")
})
app.Get("/clear", func(ctx iris.Context) {
session := sessions.Get(ctx)
// removes all entries.
session.Clear()
})
app.Get("/update", func(ctx iris.Context) {
session := sessions.Get(ctx)
// shifts the expiration based on the session's `Lifetime`.
if err := session.Man.ShiftExpiration(ctx); err != nil {
if errors.Is(err, sessions.ErrNotFound) {
ctx.StatusCode(iris.StatusNotFound)
} else if errors.Is(err, sessions.ErrNotImplemented) {
ctx.StatusCode(iris.StatusNotImplemented)
} else {
ctx.StatusCode(iris.StatusNotModified)
}
ctx.Writef("%v", err)
ctx.Application().Logger().Error(err)
}
})
app.Get("/destroy", func(ctx iris.Context) {
session := sessions.Get(ctx)
// Man(anager)'s Destroy, removes the entire session data and cookie
session.Man.Destroy(ctx)
})
// Note about Destroy:
//
// You can destroy a session outside of a handler too, using the:
// sess.DestroyByID
// sess.DestroyAll
// remember: slices and maps are muttable by-design
// The `SetImmutable` makes sure that they will be stored and received
// as immutable, so you can't change them directly by mistake.
//
// Use `SetImmutable` consistently, it's slower than `Set`.
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
app.Get("/set-immutable", func(ctx iris.Context) {
session := sessions.Get(ctx)
business := []BusinessModel{{Name: "Edward"}, {Name: "value 2"}}
session.SetImmutable("businessEdit", business)
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.
businessGet[0].Name = "Gabriel"
})
app.Get("/get-immutable", func(ctx iris.Context) {
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 := 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")
}
ctx.Writef("[]businessModel[0].Name remains: %s", firstModel.Name)
// the name should remains "Edward"
})
return app
}