2017-02-15 19:06:19 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
"github.com/kataras/iris"
|
|
|
|
"github.com/kataras/iris/sessions"
|
2017-02-15 19:06:19 +01:00
|
|
|
)
|
|
|
|
|
2017-06-08 13:39:55 +02:00
|
|
|
type businessModel struct {
|
|
|
|
Name string
|
2017-06-08 02:39:15 +02:00
|
|
|
}
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2017-07-10 17:32:42 +02:00
|
|
|
sess := sessions.New(sessions.Config{
|
2017-02-15 19:06:19 +01:00
|
|
|
// Cookie string, the session's client cookie name, for example: "mysessionid"
|
|
|
|
//
|
|
|
|
// Defaults to "irissessionid"
|
|
|
|
Cookie: "mysessionid",
|
|
|
|
// it's time.Duration, from the time cookie is created, how long it can be alive?
|
|
|
|
// 0 means no expire.
|
|
|
|
// -1 means expire when browser closes
|
|
|
|
// or set a value, like 2 hours:
|
|
|
|
Expires: time.Hour * 2,
|
|
|
|
// if you want to invalid cookies on different subdomains
|
2017-12-24 05:24:45 +01:00
|
|
|
// of the same host, then enable it.
|
|
|
|
// Defaults to false.
|
2019-07-24 18:51:42 +02:00
|
|
|
DisableSubdomainPersistence: false,
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2019-07-24 18:51:42 +02:00
|
|
|
app.Use(sess.Handler()) // session is always non-nil inside handlers now.
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
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>")
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
2019-07-24 18:51:42 +02:00
|
|
|
|
2019-08-17 09:06:20 +02:00
|
|
|
// set session values.
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/set", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
session := sessions.Get(ctx)
|
|
|
|
session.Set("name", "iris")
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2019-08-17 09:06:20 +02:00
|
|
|
// test if set here.
|
2019-07-24 18:51:42 +02:00
|
|
|
ctx.Writef("All ok session set to: %s", session.GetString("name"))
|
2017-06-08 02:39:15 +02:00
|
|
|
|
|
|
|
// Set will set the value as-it-is,
|
|
|
|
// if it's a slice or map
|
|
|
|
// you will be able to change it on .Get directly!
|
|
|
|
// Keep note that I don't recommend saving big data neither slices or maps on a session
|
|
|
|
// but if you really need it then use the `SetImmutable` instead of `Set`.
|
|
|
|
// Use `SetImmutable` consistently, it's slower.
|
|
|
|
// Read more about muttable and immutable go types: https://stackoverflow.com/a/8021081
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2019-07-24 18:51:42 +02:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/get", func(ctx iris.Context) {
|
2017-12-24 05:24:45 +01:00
|
|
|
// get a specific value, as string,
|
|
|
|
// if not found then it returns just an empty string.
|
2019-07-24 18:51:42 +02:00
|
|
|
name := sessions.Get(ctx).GetString("name")
|
2017-02-15 19:06:19 +01:00
|
|
|
|
|
|
|
ctx.Writef("The name on the /set was: %s", name)
|
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/delete", func(ctx iris.Context) {
|
2017-02-15 19:06:19 +01:00
|
|
|
// delete a specific key
|
2019-07-24 18:51:42 +02:00
|
|
|
sessions.Get(ctx).Delete("name")
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/clear", func(ctx iris.Context) {
|
2017-12-24 05:24:45 +01:00
|
|
|
// removes all entries.
|
2019-07-24 18:51:42 +02:00
|
|
|
sessions.Get(ctx).Clear()
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/update", func(ctx iris.Context) {
|
2017-12-24 05:24:45 +01:00
|
|
|
// updates expire date.
|
2017-08-14 15:21:51 +02:00
|
|
|
sess.ShiftExpiration(ctx)
|
2017-08-01 07:34:18 +02:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/destroy", func(ctx iris.Context) {
|
2019-08-17 09:06:20 +02:00
|
|
|
// destroy, removes the entire session data and cookie
|
2019-07-24 18:51:42 +02:00
|
|
|
// sess.Destroy(ctx)
|
|
|
|
// or
|
|
|
|
sessions.Get(ctx).Destroy()
|
2017-03-18 22:43:04 +01:00
|
|
|
})
|
|
|
|
// Note about Destroy:
|
2017-02-15 19:06:19 +01:00
|
|
|
//
|
|
|
|
// You can destroy a session outside of a handler too, using the:
|
2019-07-24 18:51:42 +02:00
|
|
|
// sess.DestroyByID
|
|
|
|
// sess.DestroyAll
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-06-08 14:19:04 +02:00
|
|
|
// 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
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/set_immutable", func(ctx iris.Context) {
|
2017-06-08 13:39:55 +02:00
|
|
|
business := []businessModel{{Name: "Edward"}, {Name: "value 2"}}
|
2019-07-24 18:51:42 +02:00
|
|
|
session := sessions.Get(ctx)
|
|
|
|
session.SetImmutable("businessEdit", business)
|
|
|
|
businessGet := session.Get("businessEdit").([]businessModel)
|
2017-06-08 14:19:04 +02:00
|
|
|
|
|
|
|
// 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.
|
2017-06-08 13:39:55 +02:00
|
|
|
businessGet[0].Name = "Gabriel"
|
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/get_immutable", func(ctx iris.Context) {
|
2019-07-24 18:51:42 +02:00
|
|
|
valSlice := sessions.Get(ctx).Get("businessEdit")
|
2017-06-08 14:19:04 +02:00
|
|
|
if valSlice == nil {
|
|
|
|
ctx.HTML("please navigate to the <a href='/set_immutable'>/set_immutable</a> first")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
firstModel := valSlice.([]businessModel)[0]
|
|
|
|
// businessGet[0].Name is equal to Edward initially
|
|
|
|
if firstModel.Name != "Edward" {
|
2017-06-08 13:39:55 +02:00
|
|
|
panic("Report this as a bug, immutable data cannot be changed from the caller without re-SetImmutable")
|
|
|
|
}
|
2017-06-08 14:19:04 +02:00
|
|
|
|
|
|
|
ctx.Writef("[]businessModel[0].Name remains: %s", firstModel.Name)
|
|
|
|
|
2017-06-08 13:39:55 +02:00
|
|
|
// the name should remains "Edward"
|
|
|
|
})
|
|
|
|
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
app.Run(iris.Addr(":8080"))
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|