diff options
Diffstat (limited to 'middleware')
-rw-r--r-- | middleware/context.go | 7 | ||||
-rw-r--r-- | middleware/request_id.go | 5 | ||||
-rw-r--r-- | middleware/session/bbolt.go | 70 | ||||
-rw-r--r-- | middleware/session/context.go | 9 | ||||
-rw-r--r-- | middleware/session/memstore.go | 30 | ||||
-rw-r--r-- | middleware/session/session.go | 128 | ||||
-rw-r--r-- | middleware/session/store.go | 9 |
7 files changed, 3 insertions, 255 deletions
diff --git a/middleware/context.go b/middleware/context.go deleted file mode 100644 index 8671140..0000000 --- a/middleware/context.go +++ /dev/null @@ -1,7 +0,0 @@ -package middleware - -type ctxKey int - -const ( - requestIDKey ctxKey = iota -) diff --git a/middleware/request_id.go b/middleware/request_id.go index 016b44a..feb5afa 100644 --- a/middleware/request_id.go +++ b/middleware/request_id.go @@ -4,6 +4,7 @@ import ( "context" "net/http" + "go.neonxp.ru/mux/ctxlib" "go.neonxp.ru/objectid" ) @@ -17,12 +18,12 @@ func RequestID(next http.Handler) http.Handler { requestID = objectid.New().String() } - next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), requestIDKey, requestID))) + next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxlib.RequestID, requestID))) }) } func GetRequestID(r *http.Request) string { - rid := r.Context().Value(requestIDKey) + rid := r.Context().Value(ctxlib.RequestID) if rid == nil { return "" } 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)) - }) -} diff --git a/middleware/session/context.go b/middleware/session/context.go deleted file mode 100644 index 870957d..0000000 --- a/middleware/session/context.go +++ /dev/null @@ -1,9 +0,0 @@ -package session - -type ctxKey int - -const ( - sessionManagerKey ctxKey = iota - sessionIDKey - sessionValueKey -) diff --git a/middleware/session/memstore.go b/middleware/session/memstore.go deleted file mode 100644 index d8cb958..0000000 --- a/middleware/session/memstore.go +++ /dev/null @@ -1,30 +0,0 @@ -package session - -import ( - "sync" -) - -type MemoryStore struct { - store sync.Map -} - -func (s *MemoryStore) Load(sessionID string) Values { - val, ok := s.store.Load(sessionID) - if ok { - return val.(Values) - } - - return Values{} -} - -func (s *MemoryStore) Save(sessionID string, value Values) error { - s.store.Store(sessionID, value) - - return nil -} - -func (s *MemoryStore) Remove(sessionID string) error { - s.store.Delete(sessionID) - - return nil -} diff --git a/middleware/session/session.go b/middleware/session/session.go deleted file mode 100644 index 47fc0fb..0000000 --- a/middleware/session/session.go +++ /dev/null @@ -1,128 +0,0 @@ -package session - -import ( - "context" - "errors" - "net/http" - "time" - - "go.neonxp.ru/mux" - "go.neonxp.ru/objectid" -) - -type Config struct { - SessionCookie string - Path string - Domain string - Secure bool - HttpOnly bool - MaxAge time.Duration -} - -var DefaultConfig Config = Config{ - SessionCookie: "_session", - Path: "/", - Domain: "", - Secure: false, - HttpOnly: true, - MaxAge: 365 * 24 * time.Hour, -} - -var ( - ErrSessionNotFound = errors.New("session not found") - ErrNoSessionInContext = errors.New("no session in context") -) - -type SessionManager struct { - config *Config - storer Store -} - -func New(storer Store) *SessionManager { - return NewWithConfig(&DefaultConfig, storer) -} - -func NewWithConfig(config *Config, storer Store) *SessionManager { - return &SessionManager{ - config: config, - storer: storer, - } -} - -func (s *SessionManager) Middleware() mux.Middleware { - return func(h http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var ( - sessionID string - values Values - ) - cookie, err := r.Cookie(s.config.SessionCookie) - switch { - case err == nil: - sessionID = cookie.Value - values = s.storer.Load(sessionID) - case errors.Is(err, http.ErrNoCookie): - sessionID = objectid.New().String() - } - - ctx := context.WithValue(r.Context(), sessionManagerKey, s) - ctx = context.WithValue(ctx, sessionIDKey, sessionID) - ctx = context.WithValue(ctx, sessionValueKey, values) - - h.ServeHTTP(w, r.WithContext(ctx)) - }) - } -} - -func (s *SessionManager) Values(ctx context.Context) Values { - aValue := ctx.Value(sessionValueKey) - values, ok := aValue.(Values) - if !ok || values == nil { - values = Values{} - } - - return values -} - -func (s *SessionManager) Save(w http.ResponseWriter, r *http.Request, values Values) error { - aSessionID := r.Context().Value(sessionIDKey) - sessionID, ok := aSessionID.(string) - if !ok { - return ErrNoSessionInContext - } - - http.SetCookie(w, &http.Cookie{ - Name: s.config.SessionCookie, - Value: sessionID, - Path: s.config.Path, - Domain: s.config.Domain, - Secure: s.config.Secure, - HttpOnly: s.config.HttpOnly, - MaxAge: int(s.config.MaxAge.Seconds()), - }) - - return s.storer.Save(sessionID, values) -} -func (s *SessionManager) Clear(w http.ResponseWriter, r *http.Request) error { - aSessionID := r.Context().Value(sessionIDKey) - sessionID, ok := aSessionID.(string) - if !ok { - return ErrNoSessionInContext - } - - http.SetCookie(w, &http.Cookie{ - Name: s.config.SessionCookie, - Value: sessionID, - Path: s.config.Path, - Domain: s.config.Domain, - Secure: s.config.Secure, - HttpOnly: s.config.HttpOnly, - MaxAge: -1, - }) - - return s.storer.Remove(sessionID) -} - -func FromRequest(r *http.Request) *SessionManager { - return r.Context().Value(sessionManagerKey).(*SessionManager) -} diff --git a/middleware/session/store.go b/middleware/session/store.go deleted file mode 100644 index a02ba1e..0000000 --- a/middleware/session/store.go +++ /dev/null @@ -1,9 +0,0 @@ -package session - -type Store interface { - Load(sessionID string) Values - Save(sessionID string, value Values) error - Remove(sessionID string) error -} - -type Values map[string]any |