Сессии в отдельном подпакете

This commit is contained in:
Alexander NeonXP Kiryukhin 2024-07-29 02:46:54 +03:00
parent 2916082d5e
commit c261597b9a
Signed by: NeonXP
GPG key ID: 35E33E1AB7776B39
3 changed files with 27 additions and 22 deletions

View file

@ -4,8 +4,8 @@ type ctxKey int
const ( const (
requestIDKey ctxKey = iota requestIDKey ctxKey = iota
sessionIDKey SessionIDKey
sessionValueKey SessionValueKey
sessionConfigKey SessionConfigKey
sessionStorerKey SessionStorerKey
) )

View file

@ -9,7 +9,7 @@ import (
"go.etcd.io/bbolt" "go.etcd.io/bbolt"
) )
func New(db *bbolt.DB, bucketName []byte) Store { func NewBoltStore(db *bbolt.DB, bucketName []byte) Store {
return &BoltStore{ return &BoltStore{
db: db, db: db,
bucketName: bucketName, bucketName: bucketName,

View file

@ -1,16 +1,17 @@
package middleware package session
import ( import (
"context" "context"
"errors" "errors"
"net/http" "net/http"
"sync"
"go.neonxp.ru/mux" "go.neonxp.ru/mux"
"go.neonxp.ru/mux/middleware/session" "go.neonxp.ru/mux/middleware"
"go.neonxp.ru/objectid" "go.neonxp.ru/objectid"
) )
type SessionConfig struct { type Config struct {
SessionCookie string SessionCookie string
Path string Path string
Domain string Domain string
@ -19,7 +20,7 @@ type SessionConfig struct {
MaxAge int MaxAge int
} }
var DefaultSessionConfig SessionConfig = SessionConfig{ var DefaultConfig Config = Config{
SessionCookie: "_session", SessionCookie: "_session",
Path: "/", Path: "/",
Domain: "", Domain: "",
@ -28,12 +29,16 @@ var DefaultSessionConfig SessionConfig = SessionConfig{
MaxAge: 30 * 3600, 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 func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var ( var (
sessionID string sessionID string
values session.Value values Value
) )
cookie, err := r.Cookie(config.SessionCookie) cookie, err := r.Cookie(config.SessionCookie)
switch { switch {
@ -42,7 +47,7 @@ func Session(config SessionConfig, storer session.Store) mux.Middleware {
values = storer.Load(r.Context(), sessionID) values = storer.Load(r.Context(), sessionID)
case errors.Is(err, http.ErrNoCookie): case errors.Is(err, http.ErrNoCookie):
sessionID = objectid.New().String() sessionID = objectid.New().String()
values = session.Value{} values = Value{}
} }
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
@ -55,10 +60,10 @@ func Session(config SessionConfig, storer session.Store) mux.Middleware {
MaxAge: config.MaxAge, MaxAge: config.MaxAge,
}) })
ctx := context.WithValue(r.Context(), sessionValueKey, &values) ctx := context.WithValue(r.Context(), middleware.SessionValueKey, &values)
ctx = context.WithValue(ctx, sessionIDKey, sessionID) ctx = context.WithValue(ctx, middleware.SessionIDKey, sessionID)
ctx = context.WithValue(ctx, sessionConfigKey, config) ctx = context.WithValue(ctx, middleware.SessionConfigKey, config)
ctx = context.WithValue(ctx, sessionStorerKey, storer) ctx = context.WithValue(ctx, middleware.SessionStorerKey, storer)
h.ServeHTTP(w, r.WithContext(ctx)) 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 { func FromRequest(r *http.Request) *Value {
return r.Context().Value(sessionValueKey).(*session.Value) return r.Context().Value(middleware.SessionValueKey).(*Value)
} }
func ClearSession(w http.ResponseWriter, r *http.Request) { func Clear(w http.ResponseWriter, r *http.Request) {
storer := r.Context().Value(sessionStorerKey).(session.Store) storer := r.Context().Value(middleware.SessionStorerKey).(Store)
sessionID := r.Context().Value(sessionIDKey).(string) sessionID := r.Context().Value(middleware.SessionIDKey).(string)
storer.Remove(r.Context(), sessionID) storer.Remove(r.Context(), sessionID)
config := r.Context().Value(sessionConfigKey).(SessionConfig) config := r.Context().Value(middleware.SessionConfigKey).(Config)
http.SetCookie(w, &http.Cookie{ http.SetCookie(w, &http.Cookie{
Name: config.SessionCookie, Name: config.SessionCookie,
Value: sessionID, Value: sessionID,