Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filesystem store backend #251

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Gin middleware for session management with multi-backend support:
- [GORM](#gorm)
- [memstore](#memstore)
- [PostgreSQL](#postgresql)
- [Filesystem](#Filesystem)

## Usage

Expand Down Expand Up @@ -456,3 +457,40 @@ func main() {
r.Run(":8000")
}
```

### Filesystem

```go
package main

import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/filesystem"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()

var sessionPath = "/tmp/" // in case of empty string, the system's default tmp folder is used

store := filesystem.NewStore(sessionPath,[]byte("secret"))
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")
}
```
30 changes: 30 additions & 0 deletions _example/filesystem/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
sessions "github.com/geschke/gin-contrib-sessions"
"github.com/geschke/gin-contrib-sessions/filesystem"
"github.com/gin-gonic/gin"
)

func main() {
sessionPath := "/tmp/"
r := gin.Default()
store := filesystem.NewStore(sessionPath, []byte("secret"))
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")
}
31 changes: 31 additions & 0 deletions filesystem/filesystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package filesystem

import (
"github.com/gin-contrib/sessions"
gsessions "github.com/gorilla/sessions"
)

type Store interface {
sessions.Store
}

// Keys are defined in pairs to allow key rotation, but the common case is to set a single
// authentication key and optionally an encryption key.
//
// The first key in a pair is used for authentication and the second for encryption. The
// encryption key can be set to nil or omitted in the last pair, but the authentication key
// is required in all pairs.
//
// It is recommended to use an authentication key with 32 or 64 bytes. The encryption key,
// if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.
func NewStore(path string, keyPairs ...[]byte) Store {
return &store{gsessions.NewFilesystemStore(path, keyPairs...)}
}

type store struct {
*gsessions.FilesystemStore
}

func (c *store) Options(options sessions.Options) {
c.FilesystemStore.Options = options.ToGorillaOptions()
}
41 changes: 41 additions & 0 deletions filesystem/filesystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package filesystem

import (
"os"
"testing"

"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/filesystem"
"github.com/gin-contrib/sessions/tester"
)

var sessionPath = os.TempDir()

var newStore = func(_ *testing.T) sessions.Store {
store := filesystem.NewStore(sessionPath, []byte("secret"))
return store
}

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

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

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

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

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

func TestFilesystem_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/memcachier/mc v2.0.1+incompatible h1:s8EDz0xrJLP8goitwZOoq1vA/sm0fPS4X3KAF0nyhWQ=
github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
Loading