forked from gin-contrib/sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
36 lines (32 loc) · 728 Bytes
/
main.go
File metadata and controls
36 lines (32 loc) · 728 Bytes
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
package main
import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongomgo"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
)
func main() {
r := gin.Default()
session, err := mgo.Dial("localhost:27017/test")
if err != nil {
// handle err
}
c := session.DB("").C("sessions")
store := mongomgo.NewStore(c, 3600, true, []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}