Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage.out
vendor/*
!/vendor/vendor.json
/gorm/test.db
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Gin middleware for session management with multi-backend support:
- [Redis](#redis)
- [memcached](#memcached)
- [MongoDB](#mongodb)
- [GoRM](#gorm)
- [memstore](#memstore)
- [PostgreSQL](#postgresql)

Expand Down Expand Up @@ -322,6 +323,48 @@ func main() {
}
```

### GoRM

[embedmd]:# (_example/gorm/main.go go)
```go
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")
}
```

### PostgreSQL

```go
Expand Down
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")
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ require (
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
github.com/gomodule/redigo v2.0.0+incompatible
github.com/gorilla/context v1.1.1
github.com/gorilla/sessions v1.2.0
github.com/gorilla/sessions v1.2.1
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b
github.com/lib/pq v1.10.3 // indirect
github.com/memcachier/mc v2.0.1+incompatible
github.com/pkg/errors v0.9.1 // indirect
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b
github.com/stretchr/testify v1.7.0 // indirect
github.com/wader/gormstore/v2 v2.0.0
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.12
)
196 changes: 190 additions & 6 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions gorm/gorm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gorm

import (
"time"

"github.com/gin-contrib/sessions"
"github.com/wader/gormstore/v2"
"gorm.io/gorm"
)

type Store interface {
sessions.Store
}

func NewStore(d *gorm.DB, expiredSessionCleanup bool, keyPairs ...[]byte) Store {
s := gormstore.New(d, keyPairs...)
if expiredSessionCleanup {
quit := make(chan struct{})
go s.PeriodicCleanup(1*time.Hour, quit)
}
return &store{s}
}

type store struct {
*gormstore.Store
}

func (s *store) Options(options sessions.Options) {
s.Store.SessionOpts = options.ToGorillaOptions()
}
44 changes: 44 additions & 0 deletions gorm/gorm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build go1.13

package gorm

import (
"testing"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/tester"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

var newStore = func(_ *testing.T) sessions.Store {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic(err)
}
return NewStore(db, true, []byte("secret"))
}

func TestGorm_SessionGetSet(t *testing.T) {
tester.GetSet(t, newStore)
}

func TestGorm_SessionDeleteKey(t *testing.T) {
tester.DeleteKey(t, newStore)
}

func TestGorm_SessionFlashes(t *testing.T) {
tester.Flashes(t, newStore)
}

func TestGorm_SessionClear(t *testing.T) {
tester.Clear(t, newStore)
}

func TestGorm_SessionOptions(t *testing.T) {
tester.Options(t, newStore)
}

func TestGorm_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}