2017-09-24 14:32:16 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 01:27:02 +03:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-09-24 14:32:16 +03:00
|
|
|
|
2019-10-25 01:27:02 +03:00
|
|
|
"github.com/kataras/iris/v12/sessions"
|
|
|
|
"github.com/kataras/iris/v12/sessions/sessiondb/badger"
|
2020-05-07 07:34:17 +03:00
|
|
|
|
|
|
|
"github.com/kataras/iris/v12/_examples/sessions/overview/example"
|
2017-09-24 14:32:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
db, err := badger.New("./data")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close and unlock the database when control+C/cmd+C pressed
|
|
|
|
iris.RegisterOnInterrupt(func() {
|
|
|
|
db.Close()
|
|
|
|
})
|
|
|
|
|
2018-04-22 13:52:36 +03:00
|
|
|
defer db.Close() // close and unlock the database if application errored.
|
|
|
|
|
2020-05-18 18:43:39 +03:00
|
|
|
// The default transcoder is the JSON one,
|
|
|
|
// based on the https://golang.org/pkg/encoding/json/#Unmarshal
|
|
|
|
// you can only retrieve numbers as float64 types:
|
|
|
|
// * bool, for booleans
|
|
|
|
// * float64, for numbers
|
|
|
|
// * string, for strings
|
|
|
|
// * []interface{}, for arrays
|
|
|
|
// * map[string]interface{}, for objects.
|
|
|
|
// If you want to save the data per go-specific types
|
|
|
|
// you should change the DefaultTranscoder to the GobTranscoder one:
|
|
|
|
// sessions.DefaultTranscoder = sessions.GobTranscoder{}
|
|
|
|
|
2017-09-24 14:32:16 +03:00
|
|
|
sess := sessions.New(sessions.Config{
|
2018-06-25 20:21:52 +03:00
|
|
|
Cookie: "sessionscookieid",
|
2020-05-17 22:08:43 +03:00
|
|
|
Expires: 1 * time.Minute, // <=0 means unlimited life. Defaults to 0.
|
2018-06-25 20:21:52 +03:00
|
|
|
AllowReclaim: true,
|
2017-09-24 14:32:16 +03:00
|
|
|
})
|
|
|
|
|
2020-05-17 22:08:43 +03:00
|
|
|
sess.OnDestroy(func(sid string) {
|
|
|
|
println(sid + " expired and destroyed from memory and its values from database")
|
|
|
|
})
|
|
|
|
|
2017-09-24 14:32:16 +03:00
|
|
|
//
|
|
|
|
// IMPORTANT:
|
|
|
|
//
|
|
|
|
sess.UseDatabase(db)
|
|
|
|
|
2020-05-07 07:34:17 +03:00
|
|
|
app := example.NewApp(sess)
|
2020-04-28 05:22:58 +03:00
|
|
|
app.Listen(":8080")
|
2017-09-24 14:32:16 +03:00
|
|
|
}
|