Skip to content

Commit

Permalink
fix: forgot to "git add" the example file
Browse files Browse the repository at this point in the history
  • Loading branch information
TJM committed Mar 31, 2021
1 parent 5cce4eb commit cf37429
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions _example/gorm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic(err)
}
store := gormsessions.NewStore(db, true, []byte("secret"))

r := gin.Default()
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")
}

0 comments on commit cf37429

Please sign in to comment.