summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-29 02:46:54 +0300
committerAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-29 02:46:54 +0300
commitc261597b9adb827bfa16de8f21ce47ac63bec138 (patch)
tree6f9af51e9d13b9f6e318e53de2cca7b2dc18b963
parent2916082d5ed94ef86ad58bdb7256ae07b214c4f3 (diff)
Сессии в отдельном подпакете
-rw-r--r--middleware/context.go8
-rw-r--r--middleware/session/bbolt.go2
-rw-r--r--middleware/session/session.go (renamed from middleware/session.go)39
3 files changed, 27 insertions, 22 deletions
diff --git a/middleware/context.go b/middleware/context.go
index b9ad45f..a76bca0 100644
--- a/middleware/context.go
+++ b/middleware/context.go
@@ -4,8 +4,8 @@ type ctxKey int
const (
requestIDKey ctxKey = iota
- sessionIDKey
- sessionValueKey
- sessionConfigKey
- sessionStorerKey
+ SessionIDKey
+ SessionValueKey
+ SessionConfigKey
+ SessionStorerKey
)
diff --git a/middleware/session/bbolt.go b/middleware/session/bbolt.go
index 1068ed8..be484d7 100644
--- a/middleware/session/bbolt.go
+++ b/middleware/session/bbolt.go
@@ -9,7 +9,7 @@ import (
"go.etcd.io/bbolt"
)
-func New(db *bbolt.DB, bucketName []byte) Store {
+func NewBoltStore(db *bbolt.DB, bucketName []byte) Store {
return &BoltStore{
db: db,
bucketName: bucketName,
diff --git a/middleware/session.go b/middleware/session/session.go
index 838e088..4d02bf1 100644
--- a/middleware/session.go
+++ b/middleware/session/session.go
@@ -1,16 +1,17 @@
-package middleware
+package session
import (
"context"
"errors"
"net/http"
+ "sync"
"go.neonxp.ru/mux"
- "go.neonxp.ru/mux/middleware/session"
+ "go.neonxp.ru/mux/middleware"
"go.neonxp.ru/objectid"
)
-type SessionConfig struct {
+type Config struct {
SessionCookie string
Path string
Domain string
@@ -19,7 +20,7 @@ type SessionConfig struct {
MaxAge int
}
-var DefaultSessionConfig SessionConfig = SessionConfig{
+var DefaultConfig Config = Config{
SessionCookie: "_session",
Path: "/",
Domain: "",
@@ -28,12 +29,16 @@ var DefaultSessionConfig SessionConfig = SessionConfig{
MaxAge: 30 * 3600,
}
-func Session(config SessionConfig, storer session.Store) mux.Middleware {
+func Middleware(config Config, storer Store) mux.Middleware {
+ if storer == nil {
+ storer = &MemoryStore{store: sync.Map{}}
+ }
+
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
sessionID string
- values session.Value
+ values Value
)
cookie, err := r.Cookie(config.SessionCookie)
switch {
@@ -42,7 +47,7 @@ func Session(config SessionConfig, storer session.Store) mux.Middleware {
values = storer.Load(r.Context(), sessionID)
case errors.Is(err, http.ErrNoCookie):
sessionID = objectid.New().String()
- values = session.Value{}
+ values = Value{}
}
http.SetCookie(w, &http.Cookie{
@@ -55,10 +60,10 @@ func Session(config SessionConfig, storer session.Store) mux.Middleware {
MaxAge: config.MaxAge,
})
- ctx := context.WithValue(r.Context(), sessionValueKey, &values)
- ctx = context.WithValue(ctx, sessionIDKey, sessionID)
- ctx = context.WithValue(ctx, sessionConfigKey, config)
- ctx = context.WithValue(ctx, sessionStorerKey, storer)
+ ctx := context.WithValue(r.Context(), middleware.SessionValueKey, &values)
+ ctx = context.WithValue(ctx, middleware.SessionIDKey, sessionID)
+ ctx = context.WithValue(ctx, middleware.SessionConfigKey, config)
+ ctx = context.WithValue(ctx, middleware.SessionStorerKey, storer)
h.ServeHTTP(w, r.WithContext(ctx))
@@ -68,15 +73,15 @@ func Session(config SessionConfig, storer session.Store) mux.Middleware {
}
}
-func SessionFromRequest(r *http.Request) *session.Value {
- return r.Context().Value(sessionValueKey).(*session.Value)
+func FromRequest(r *http.Request) *Value {
+ return r.Context().Value(middleware.SessionValueKey).(*Value)
}
-func ClearSession(w http.ResponseWriter, r *http.Request) {
- storer := r.Context().Value(sessionStorerKey).(session.Store)
- sessionID := r.Context().Value(sessionIDKey).(string)
+func Clear(w http.ResponseWriter, r *http.Request) {
+ storer := r.Context().Value(middleware.SessionStorerKey).(Store)
+ sessionID := r.Context().Value(middleware.SessionIDKey).(string)
storer.Remove(r.Context(), sessionID)
- config := r.Context().Value(sessionConfigKey).(SessionConfig)
+ config := r.Context().Value(middleware.SessionConfigKey).(Config)
http.SetCookie(w, &http.Cookie{
Name: config.SessionCookie,
Value: sessionID,