-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
main.go
56 lines (44 loc) · 1.37 KB
/
main.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
package main
import (
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/sessions"
"github.com/kataras/iris/v12/sessions/sessiondb/badger"
"github.com/kataras/iris/v12/_examples/sessions/overview/example"
)
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()
})
defer db.Close() // close and unlock the database if application errored.
// 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{}
sess := sessions.New(sessions.Config{
Cookie: "sessionscookieid",
Expires: 1 * time.Minute, // <=0 means unlimited life. Defaults to 0.
AllowReclaim: true,
})
sess.OnDestroy(func(sid string) {
println(sid + " expired and destroyed from memory and its values from database")
})
//
// IMPORTANT:
//
sess.UseDatabase(db)
app := example.NewApp(sess)
app.Listen(":8080")
}