Skip to content

Commit bd1e07d

Browse files
author
Tommy McNeely
authored
feat: Add GoRM session store (#131)
1 parent 02e9e66 commit bd1e07d

File tree

7 files changed

+348
-9
lines changed

7 files changed

+348
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage.out
22
vendor/*
33
!/vendor/vendor.json
4+
/gorm/test.db

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Gin middleware for session management with multi-backend support:
1313
- [Redis](#redis)
1414
- [memcached](#memcached)
1515
- [MongoDB](#mongodb)
16+
- [GoRM](#gorm)
1617
- [memstore](#memstore)
1718
- [PostgreSQL](#postgresql)
1819

@@ -322,6 +323,48 @@ func main() {
322323
}
323324
```
324325

326+
### GoRM
327+
328+
[embedmd]:# (_example/gorm/main.go go)
329+
```go
330+
package main
331+
332+
import (
333+
"github.com/gin-contrib/sessions"
334+
gormsessions "github.com/gin-contrib/sessions/gorm"
335+
"github.com/gin-gonic/gin"
336+
"gorm.io/driver/sqlite"
337+
"gorm.io/gorm"
338+
)
339+
340+
func main() {
341+
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
342+
if err != nil {
343+
panic(err)
344+
}
345+
store := gormsessions.NewStore(db, true, []byte("secret"))
346+
347+
r := gin.Default()
348+
r.Use(sessions.Sessions("mysession", store))
349+
350+
r.GET("/incr", func(c *gin.Context) {
351+
session := sessions.Default(c)
352+
var count int
353+
v := session.Get("count")
354+
if v == nil {
355+
count = 0
356+
} else {
357+
count = v.(int)
358+
count++
359+
}
360+
session.Set("count", count)
361+
session.Save()
362+
c.JSON(200, gin.H{"count": count})
363+
})
364+
r.Run(":8000")
365+
}
366+
```
367+
325368
### PostgreSQL
326369

327370
```go

_example/gorm/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"github.com/gin-contrib/sessions"
5+
gormsessions "github.com/gin-contrib/sessions/gorm"
6+
"github.com/gin-gonic/gin"
7+
"gorm.io/driver/sqlite"
8+
"gorm.io/gorm"
9+
)
10+
11+
func main() {
12+
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
13+
if err != nil {
14+
panic(err)
15+
}
16+
store := gormsessions.NewStore(db, true, []byte("secret"))
17+
18+
r := gin.Default()
19+
r.Use(sessions.Sessions("mysession", store))
20+
21+
r.GET("/incr", func(c *gin.Context) {
22+
session := sessions.Default(c)
23+
var count int
24+
v := session.Get("count")
25+
if v == nil {
26+
count = 0
27+
} else {
28+
count = v.(int)
29+
count++
30+
}
31+
session.Set("count", count)
32+
session.Save()
33+
c.JSON(200, gin.H{"count": count})
34+
})
35+
r.Run(":8000")
36+
}

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ require (
1111
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
1212
github.com/gomodule/redigo v2.0.0+incompatible
1313
github.com/gorilla/context v1.1.1
14-
github.com/gorilla/sessions v1.2.0
14+
github.com/gorilla/sessions v1.2.1
1515
github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b
1616
github.com/lib/pq v1.10.3 // indirect
1717
github.com/memcachier/mc v2.0.1+incompatible
18-
github.com/pkg/errors v0.9.1 // indirect
1918
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b
20-
github.com/stretchr/testify v1.7.0 // indirect
19+
github.com/wader/gormstore/v2 v2.0.0
20+
gorm.io/driver/sqlite v1.1.4
21+
gorm.io/gorm v1.20.12
2122
)

go.sum

Lines changed: 190 additions & 6 deletions
Large diffs are not rendered by default.

gorm/gorm.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package gorm
2+
3+
import (
4+
"time"
5+
6+
"github.com/gin-contrib/sessions"
7+
"github.com/wader/gormstore/v2"
8+
"gorm.io/gorm"
9+
)
10+
11+
type Store interface {
12+
sessions.Store
13+
}
14+
15+
func NewStore(d *gorm.DB, expiredSessionCleanup bool, keyPairs ...[]byte) Store {
16+
s := gormstore.New(d, keyPairs...)
17+
if expiredSessionCleanup {
18+
quit := make(chan struct{})
19+
go s.PeriodicCleanup(1*time.Hour, quit)
20+
}
21+
return &store{s}
22+
}
23+
24+
type store struct {
25+
*gormstore.Store
26+
}
27+
28+
func (s *store) Options(options sessions.Options) {
29+
s.Store.SessionOpts = options.ToGorillaOptions()
30+
}

gorm/gorm_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// +build go1.13
2+
3+
package gorm
4+
5+
import (
6+
"testing"
7+
8+
"github.com/gin-contrib/sessions"
9+
"github.com/gin-contrib/sessions/tester"
10+
"gorm.io/driver/sqlite"
11+
"gorm.io/gorm"
12+
)
13+
14+
var newStore = func(_ *testing.T) sessions.Store {
15+
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
16+
if err != nil {
17+
panic(err)
18+
}
19+
return NewStore(db, true, []byte("secret"))
20+
}
21+
22+
func TestGorm_SessionGetSet(t *testing.T) {
23+
tester.GetSet(t, newStore)
24+
}
25+
26+
func TestGorm_SessionDeleteKey(t *testing.T) {
27+
tester.DeleteKey(t, newStore)
28+
}
29+
30+
func TestGorm_SessionFlashes(t *testing.T) {
31+
tester.Flashes(t, newStore)
32+
}
33+
34+
func TestGorm_SessionClear(t *testing.T) {
35+
tester.Clear(t, newStore)
36+
}
37+
38+
func TestGorm_SessionOptions(t *testing.T) {
39+
tester.Options(t, newStore)
40+
}
41+
42+
func TestGorm_SessionMany(t *testing.T) {
43+
tester.Many(t, newStore)
44+
}

0 commit comments

Comments
 (0)