-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tommy McNeely
committed
Mar 26, 2022
1 parent
02e9e66
commit bd1e07d
Showing
7 changed files
with
348 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
coverage.out | ||
vendor/* | ||
!/vendor/vendor.json | ||
/gorm/test.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |