2017-09-24 13:32:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-09-24 13:32:16 +02:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/sessions"
|
|
|
|
"github.com/kataras/iris/v12/sessions/sessiondb/badger"
|
2020-05-07 06:34:17 +02:00
|
|
|
|
|
|
|
"github.com/kataras/iris/v12/_examples/sessions/overview/example"
|
2017-09-24 13:32:16 +02: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 12:52:36 +02:00
|
|
|
defer db.Close() // close and unlock the database if application errored.
|
|
|
|
|
2020-05-18 17:43:39 +02: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 13:32:16 +02:00
|
|
|
sess := sessions.New(sessions.Config{
|
2018-06-25 19:21:52 +02:00
|
|
|
Cookie: "sessionscookieid",
|
2020-05-17 21:08:43 +02:00
|
|
|
Expires: 1 * time.Minute, // <=0 means unlimited life. Defaults to 0.
|
2018-06-25 19:21:52 +02:00
|
|
|
AllowReclaim: true,
|
2017-09-24 13:32:16 +02:00
|
|
|
})
|
|
|
|
|
2020-05-17 21:08:43 +02:00
|
|
|
sess.OnDestroy(func(sid string) {
|
|
|
|
println(sid + " expired and destroyed from memory and its values from database")
|
|
|
|
})
|
|
|
|
|
2017-09-24 13:32:16 +02:00
|
|
|
//
|
|
|
|
// IMPORTANT:
|
|
|
|
//
|
|
|
|
sess.UseDatabase(db)
|
|
|
|
|
2020-05-07 06:34:17 +02:00
|
|
|
app := example.NewApp(sess)
|
2020-04-28 04:22:58 +02:00
|
|
|
app.Listen(":8080")
|
2017-09-24 13:32:16 +02:00
|
|
|
}
|