summaryrefslogtreecommitdiff
path: root/middleware/session/bbolt.go
diff options
context:
space:
mode:
authorAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-09-17 01:19:25 +0300
committerAlexander Neonxp Kiryukhin <i@neonxp.ru>2024-09-17 01:19:25 +0300
commita054f480adf26e90ddcb66408688486704ab7094 (patch)
tree4600b6e7633979e1a4b94cfcaeb7ddf156733575 /middleware/session/bbolt.go
parent5094ebf213243023c6325310b3f710a0974025dd (diff)
simple handlerv0.0.2
Diffstat (limited to 'middleware/session/bbolt.go')
-rw-r--r--middleware/session/bbolt.go70
1 files changed, 0 insertions, 70 deletions
diff --git a/middleware/session/bbolt.go b/middleware/session/bbolt.go
deleted file mode 100644
index bf46953..0000000
--- a/middleware/session/bbolt.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package session
-
-import (
- "bytes"
- "encoding/gob"
- "log/slog"
-
- "go.etcd.io/bbolt"
-)
-
-func NewBoltStore(db *bbolt.DB, bucketName []byte) Store {
- return &BoltStore{
- db: db,
- bucketName: bucketName,
- }
-}
-
-type BoltStore struct {
- db *bbolt.DB
- bucketName []byte
-}
-
-func (s *BoltStore) Load(sessionID string) Values {
- v := Values{}
- 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.Warn("failed load session", slog.Any("error", err))
- }
- return v
-}
-
-func (s *BoltStore) Save(sessionID string, value Values) 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(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))
- })
-}