2017-02-15 19:06:19 +01:00
|
|
|
package main
|
|
|
|
|
2017-06-07 21:14:13 +02:00
|
|
|
// developers can use any library to add a custom cookie encoder/decoder.
|
|
|
|
// At this example we use the gorilla's securecookie package:
|
|
|
|
// $ go get github.com/gorilla/securecookie
|
|
|
|
// $ go run main.go
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2017-06-07 21:14:13 +02:00
|
|
|
import (
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-07-10 17:32:42 +02:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/sessions"
|
2017-06-07 21:14:13 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/securecookie"
|
2017-02-15 19:06:19 +01:00
|
|
|
)
|
|
|
|
|
2017-07-11 17:09:32 +02:00
|
|
|
func newApp() *iris.Application {
|
2017-02-15 19:06:19 +01:00
|
|
|
app := iris.New()
|
|
|
|
|
2017-03-18 22:43:04 +01:00
|
|
|
cookieName := "mycustomsessionid"
|
|
|
|
// AES only supports key sizes of 16, 24 or 32 bytes.
|
|
|
|
// You either need to provide exactly that amount or you derive the key from what you type in.
|
|
|
|
hashKey := []byte("the-big-and-secret-fash-key-here")
|
|
|
|
blockKey := []byte("lot-secret-of-characters-big-too")
|
|
|
|
secureCookie := securecookie.New(hashKey, blockKey)
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
mySessions := sessions.New(sessions.Config{
|
2018-06-25 19:21:52 +02:00
|
|
|
Cookie: cookieName,
|
|
|
|
Encode: secureCookie.Encode,
|
|
|
|
Decode: secureCookie.Decode,
|
|
|
|
AllowReclaim: true,
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/", func(ctx iris.Context) {
|
2017-02-15 19:06:19 +01:00
|
|
|
ctx.Writef("You should navigate to the /set, /get, /delete, /clear,/destroy instead")
|
|
|
|
})
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/set", func(ctx iris.Context) {
|
2019-08-17 09:06:20 +02:00
|
|
|
// set session values
|
2017-07-10 17:32:42 +02:00
|
|
|
s := mySessions.Start(ctx)
|
|
|
|
s.Set("name", "iris")
|
2017-02-15 19:06:19 +01:00
|
|
|
|
2019-08-17 09:06:20 +02:00
|
|
|
// test if set here
|
2019-06-07 20:07:08 +02:00
|
|
|
ctx.Writef("All ok session set to: %s", s.GetString("name"))
|
2017-02-15 19:06:19 +01:00
|
|
|
})
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/get", func(ctx iris.Context) {
|
2017-02-15 19:06:19 +01:00
|
|
|
// get a specific key, as string, if no found returns just an empty string
|
2017-07-10 17:32:42 +02:00
|
|
|
s := mySessions.Start(ctx)
|
|
|
|
name := s.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
|
2017-07-10 17:32:42 +02:00
|
|
|
s := mySessions.Start(ctx)
|
|
|
|
s.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-02-15 19:06:19 +01:00
|
|
|
// removes all entries
|
2017-07-10 17:32:42 +02:00
|
|
|
mySessions.Start(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-08-01 07:34:18 +02:00
|
|
|
// updates expire date with a new date
|
2017-08-14 15:21:51 +02:00
|
|
|
mySessions.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
|
2017-07-10 17:32:42 +02:00
|
|
|
mySessions.Destroy(ctx)
|
2017-08-01 07:34:18 +02:00
|
|
|
})
|
|
|
|
// Note about destroy:
|
2017-02-15 19:06:19 +01:00
|
|
|
//
|
|
|
|
// You can destroy a session outside of a handler too, using the:
|
|
|
|
// mySessions.DestroyByID
|
|
|
|
// mySessions.DestroyAll
|
|
|
|
|
2017-07-11 17:09:32 +02:00
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := newApp()
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|