Skip to content

Commit 10bdc64

Browse files
geschkeappleboy
andauthored
feat: filesystem store backend (#251)
* Update go.mod * Update cookie.go * add filesystem store as provided by Gorilla sessions * Add filesystem backend example * add filesystem tester * add example file, add some debug output in tests * reset package / module paths, so it should ready to merge * module cleanup * conflicts resolved, ready for review * fix filesystem test --------- Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
1 parent 4327f7e commit 10bdc64

4 files changed

Lines changed: 139 additions & 0 deletions

File tree

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Gin middleware for session management with multi-backend support:
1515
- [GORM](#gorm)
1616
- [memstore](#memstore)
1717
- [PostgreSQL](#postgresql)
18+
- [Filesystem](#Filesystem)
1819

1920
## Usage
2021

@@ -506,3 +507,40 @@ func main() {
506507
r.Run(":8000")
507508
}
508509
```
510+
511+
### Filesystem
512+
513+
```go
514+
package main
515+
516+
import (
517+
"github.com/gin-contrib/sessions"
518+
"github.com/gin-contrib/sessions/filesystem"
519+
"github.com/gin-gonic/gin"
520+
)
521+
522+
func main() {
523+
r := gin.Default()
524+
525+
var sessionPath = "/tmp/" // in case of empty string, the system's default tmp folder is used
526+
527+
store := filesystem.NewStore(sessionPath,[]byte("secret"))
528+
r.Use(sessions.Sessions("mysession", store))
529+
530+
r.GET("/incr", func(c *gin.Context) {
531+
session := sessions.Default(c)
532+
var count int
533+
v := session.Get("count")
534+
if v == nil {
535+
count = 0
536+
} else {
537+
count = v.(int)
538+
count++
539+
}
540+
session.Set("count", count)
541+
session.Save()
542+
c.JSON(200, gin.H{"count": count})
543+
})
544+
r.Run(":8000")
545+
}
546+
```

_example/filesystem/main.go

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

filesystem/filesystem.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package filesystem
2+
3+
import (
4+
"github.com/gin-contrib/sessions"
5+
gsessions "github.com/gorilla/sessions"
6+
)
7+
8+
type Store interface {
9+
sessions.Store
10+
}
11+
12+
// Keys are defined in pairs to allow key rotation, but the common case is to set a single
13+
// authentication key and optionally an encryption key.
14+
//
15+
// The first key in a pair is used for authentication and the second for encryption. The
16+
// encryption key can be set to nil or omitted in the last pair, but the authentication key
17+
// is required in all pairs.
18+
//
19+
// It is recommended to use an authentication key with 32 or 64 bytes. The encryption key,
20+
// if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
21+
func NewStore(path string, keyPairs ...[]byte) Store {
22+
return &store{gsessions.NewFilesystemStore(path, keyPairs...)}
23+
}
24+
25+
type store struct {
26+
*gsessions.FilesystemStore
27+
}
28+
29+
func (c *store) Options(options sessions.Options) {
30+
c.FilesystemStore.Options = options.ToGorillaOptions()
31+
}

filesystem/filesystem_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package filesystem
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/gin-contrib/sessions"
8+
"github.com/gin-contrib/sessions/tester"
9+
)
10+
11+
var sessionPath = os.TempDir()
12+
13+
var newStore = func(_ *testing.T) sessions.Store {
14+
store := NewStore(sessionPath, []byte("secret"))
15+
return store
16+
}
17+
18+
func TestFilesystem_SessionGetSet(t *testing.T) {
19+
tester.GetSet(t, newStore)
20+
}
21+
22+
func TestFilesystem_SessionDeleteKey(t *testing.T) {
23+
tester.DeleteKey(t, newStore)
24+
}
25+
26+
func TestFilesystem_SessionFlashes(t *testing.T) {
27+
tester.Flashes(t, newStore)
28+
}
29+
30+
func TestFilesystem_SessionClear(t *testing.T) {
31+
tester.Clear(t, newStore)
32+
}
33+
34+
func TestFilesystem_SessionOptions(t *testing.T) {
35+
tester.Options(t, newStore)
36+
}
37+
38+
func TestFilesystem_SessionMany(t *testing.T) {
39+
tester.Many(t, newStore)
40+
}

0 commit comments

Comments
 (0)