summaryrefslogtreecommitdiff
path: root/middleware/session
diff options
context:
space:
mode:
Diffstat (limited to 'middleware/session')
-rw-r--r--middleware/session/bbolt.go71
-rw-r--r--middleware/session/memstore.go31
-rw-r--r--middleware/session/store.go18
3 files changed, 120 insertions, 0 deletions
diff --git a/middleware/session/bbolt.go b/middleware/session/bbolt.go
new file mode 100644
index 0000000..1068ed8
--- /dev/null
+++ b/middleware/session/bbolt.go
@@ -0,0 +1,71 @@
+package session
+
+import (
+ "bytes"
+ "context"
+ "encoding/gob"
+ "log/slog"
+
+ "go.etcd.io/bbolt"
+)
+
+func New(db *bbolt.DB, bucketName []byte) Store {
+ return &BoltStore{
+ db: db,
+ bucketName: bucketName,
+ }
+}
+
+type BoltStore struct {
+ db *bbolt.DB
+ bucketName []byte
+}
+
+func (s *BoltStore) Load(ctx context.Context, sessionID string) Value {
+ v := Value{}
+ err := s.db.View(func(tx *bbolt.Tx) error {
+ bucket := tx.Bucket(s.bucketName)
+ if bucket == nil {
+ // no bucket -- normal situation
+ return nil
+ }
+ vb := bucket.Get([]byte(sessionID))
+ if vb == nil {
+ // no session -- no error
+ return nil
+ }
+ rdr := bytes.NewBuffer(vb)
+
+ return gob.NewDecoder(rdr).Decode(&v)
+ })
+ if err != nil {
+ slog.WarnContext(ctx, "failed load session", slog.Any("error", err))
+ }
+ return v
+}
+
+func (s *BoltStore) Save(ctx context.Context, sessionID string, value Value) error {
+ return s.db.Update(func(tx *bbolt.Tx) error {
+ bucket, err := tx.CreateBucketIfNotExists(s.bucketName)
+ if err != nil {
+ return err
+ }
+ wrt := bytes.NewBuffer([]byte{})
+ if err := gob.NewEncoder(wrt).Encode(value); err != nil {
+ return err
+ }
+
+ return bucket.Put([]byte(sessionID), wrt.Bytes())
+ })
+}
+
+func (s *BoltStore) Remove(ctx context.Context, sessionID string) error {
+ return s.db.Update(func(tx *bbolt.Tx) error {
+ bucket, err := tx.CreateBucketIfNotExists(s.bucketName)
+ if err != nil {
+ return err
+ }
+
+ return bucket.Delete([]byte(sessionID))
+ })
+}
diff --git a/middleware/session/memstore.go b/middleware/session/memstore.go
new file mode 100644
index 0000000..2fcef39
--- /dev/null
+++ b/middleware/session/memstore.go
@@ -0,0 +1,31 @@
+package session
+
+import (
+ "context"
+ "sync"
+)
+
+type MemoryStore struct {
+ store sync.Map
+}
+
+func (s *MemoryStore) Load(ctx context.Context, sessionID string) Value {
+ val, ok := s.store.Load(sessionID)
+ if ok {
+ return val.(Value)
+ }
+
+ return Value{}
+}
+
+func (s *MemoryStore) Save(ctx context.Context, sessionID string, value Value) error {
+ s.store.Store(sessionID, value)
+
+ return nil
+}
+
+func (s *MemoryStore) Remove(ctx context.Context, sessionID string) error {
+ s.store.Delete(sessionID)
+
+ return nil
+}
diff --git a/middleware/session/store.go b/middleware/session/store.go
new file mode 100644
index 0000000..b74a8aa
--- /dev/null
+++ b/middleware/session/store.go
@@ -0,0 +1,18 @@
+package session
+
+import (
+ "context"
+ "errors"
+)
+
+var (
+ ErrSessionNotFound = errors.New("session not found")
+)
+
+type Store interface {
+ Load(ctx context.Context, sessionID string) Value
+ Save(ctx context.Context, sessionID string, value Value) error
+ Remove(ctx context.Context, sessionID string) error
+}
+
+type Value map[string]any