fully convert redis over to custom key value store type
This commit is contained in:
@@ -1,90 +1,90 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import (
|
// import (
|
||||||
"sync"
|
// "sync"
|
||||||
"time"
|
// "time"
|
||||||
|
|
||||||
"astraltech.xyz/accountmanager/src/logging"
|
// "astraltech.xyz/accountmanager/src/logging"
|
||||||
"astraltech.xyz/accountmanager/src/worker"
|
// "astraltech.xyz/accountmanager/src/worker"
|
||||||
)
|
// )
|
||||||
|
|
||||||
type MemoryStore struct {
|
// type MemoryStore struct {
|
||||||
sessions map[string]*SessionData
|
// sessions map[string]*SessionData
|
||||||
lock sync.RWMutex
|
// lock sync.RWMutex
|
||||||
}
|
// }
|
||||||
|
|
||||||
func NewMemoryStore() *MemoryStore {
|
// func NewMemoryStore() *MemoryStore {
|
||||||
logging.Debug("Creating new in memory session store")
|
// logging.Debug("Creating new in memory session store")
|
||||||
store := &MemoryStore{
|
// store := &MemoryStore{
|
||||||
sessions: make(map[string]*SessionData),
|
// sessions: make(map[string]*SessionData),
|
||||||
}
|
// }
|
||||||
worker.CreateWorker(time.Minute*5, store.cleanup)
|
// worker.CreateWorker(time.Minute*5, store.cleanup)
|
||||||
return store
|
// return store
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (m *MemoryStore) Create(sessionID string, session *SessionData) (err error) {
|
// func (m *MemoryStore) Create(sessionID string, session *SessionData) (err error) {
|
||||||
hashedSession := hashSession(sessionID)
|
// hashedSession := hashSession(sessionID)
|
||||||
|
|
||||||
m.lock.Lock()
|
// m.lock.Lock()
|
||||||
defer m.lock.Unlock()
|
// defer m.lock.Unlock()
|
||||||
_, exist := m.sessions[hashedSession]
|
// _, exist := m.sessions[hashedSession]
|
||||||
if exist {
|
// if exist {
|
||||||
return ErrSessionAlreadyExists
|
// return ErrSessionAlreadyExists
|
||||||
}
|
// }
|
||||||
|
|
||||||
m.sessions[hashedSession] = session
|
// m.sessions[hashedSession] = session
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
func (m *MemoryStore) Get(sessionID string) (*SessionData, error) {
|
// func (m *MemoryStore) Get(sessionID string) (*SessionData, error) {
|
||||||
m.lock.RLock()
|
// m.lock.RLock()
|
||||||
hashed := hashSession(sessionID)
|
// hashed := hashSession(sessionID)
|
||||||
data, exists := m.sessions[hashed]
|
// data, exists := m.sessions[hashed]
|
||||||
m.lock.RUnlock()
|
// m.lock.RUnlock()
|
||||||
if exists == false {
|
// if exists == false {
|
||||||
return nil, ErrSessionNotFound
|
// return nil, ErrSessionNotFound
|
||||||
}
|
// }
|
||||||
copy := *data
|
// copy := *data
|
||||||
return ©, nil
|
// return ©, nil
|
||||||
}
|
// }
|
||||||
func (m *MemoryStore) Update(sessionID string, session *SessionData) error {
|
// func (m *MemoryStore) Update(sessionID string, session *SessionData) error {
|
||||||
hashedSession := hashSession(sessionID)
|
// hashedSession := hashSession(sessionID)
|
||||||
|
|
||||||
m.lock.Lock()
|
// m.lock.Lock()
|
||||||
defer m.lock.Unlock()
|
// defer m.lock.Unlock()
|
||||||
_, exist := m.sessions[hashedSession]
|
// _, exist := m.sessions[hashedSession]
|
||||||
if !exist {
|
// if !exist {
|
||||||
return ErrSessionNotFound
|
// return ErrSessionNotFound
|
||||||
}
|
// }
|
||||||
m.sessions[hashedSession] = session
|
// m.sessions[hashedSession] = session
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (m *MemoryStore) cleanup() {
|
// func (m *MemoryStore) cleanup() {
|
||||||
logging.Debug("Cleaning up memory store sessions")
|
// logging.Debug("Cleaning up memory store sessions")
|
||||||
now := time.Now()
|
// now := time.Now()
|
||||||
|
|
||||||
m.lock.Lock()
|
// m.lock.Lock()
|
||||||
defer m.lock.Unlock()
|
// defer m.lock.Unlock()
|
||||||
|
|
||||||
deleted := 0
|
// deleted := 0
|
||||||
for id, session := range m.sessions {
|
// for id, session := range m.sessions {
|
||||||
if now.After(session.ExpiresAt) {
|
// if now.After(session.ExpiresAt) {
|
||||||
delete(m.sessions, id)
|
// delete(m.sessions, id)
|
||||||
deleted = deleted + 1
|
// deleted = deleted + 1
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
logging.Infof("Cleaned up %d stale sessions", deleted)
|
// logging.Infof("Cleaned up %d stale sessions", deleted)
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (m *MemoryStore) Delete(sessionID string) error {
|
// func (m *MemoryStore) Delete(sessionID string) error {
|
||||||
hashedSession := hashSession(sessionID)
|
// hashedSession := hashSession(sessionID)
|
||||||
|
|
||||||
m.lock.Lock()
|
// m.lock.Lock()
|
||||||
defer m.lock.Unlock()
|
// defer m.lock.Unlock()
|
||||||
_, exist := m.sessions[hashedSession]
|
// _, exist := m.sessions[hashedSession]
|
||||||
if !exist {
|
// if !exist {
|
||||||
return ErrSessionNotFound
|
// return ErrSessionNotFound
|
||||||
}
|
// }
|
||||||
delete(m.sessions, hashedSession)
|
// delete(m.sessions, hashedSession)
|
||||||
return nil
|
// return nil
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
const SessionCookieName = "session_token"
|
const SessionCookieName = "session_token"
|
||||||
|
|
||||||
type SessionManager struct {
|
type SessionManager struct {
|
||||||
store store.Store[string, *SessionData]
|
store store.KeyValueStore[*SessionData]
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance *SessionManager
|
var instance *SessionManager
|
||||||
@@ -37,12 +37,12 @@ func (manager *SessionManager) SetStoreType(storeType StoreType) {
|
|||||||
switch storeType {
|
switch storeType {
|
||||||
case InMemory:
|
case InMemory:
|
||||||
{
|
{
|
||||||
manager.store = NewMemoryStore()
|
// manager.store = NewMemoryStore()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
case Redis:
|
case Redis:
|
||||||
{
|
{
|
||||||
manager.store = NewRedisStore()
|
manager.store = store.NewRedisStore[*SessionData]()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,10 +72,10 @@ func (manager *SessionManager) CreateSession(userID string) (cookie *http.Cookie
|
|||||||
Name: SessionCookieName,
|
Name: SessionCookieName,
|
||||||
Value: token,
|
Value: token,
|
||||||
Path: "/",
|
Path: "/",
|
||||||
HttpOnly: true, // Essential: prevents JS access
|
HttpOnly: true,
|
||||||
Secure: true, // Set to TRUE in production (HTTPS)
|
Secure: true,
|
||||||
SameSite: http.SameSiteLaxMode,
|
SameSite: http.SameSiteLaxMode,
|
||||||
MaxAge: 3600, // 1 hour
|
MaxAge: 3600,
|
||||||
}
|
}
|
||||||
return newCookie, nil
|
return newCookie, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
package session
|
package session
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrSessionNotFound = errors.New("Session not found")
|
||||||
|
var ErrSessionAlreadyExists = errors.New("Session already exists")
|
||||||
|
var ErrSessionExpired = errors.New("Session expired")
|
||||||
|
var ErrSessionBackend = errors.New("Session backend")
|
||||||
|
|
||||||
type SessionData struct {
|
type SessionData struct {
|
||||||
UserID string `json:"userid"`
|
UserID string `json:"userid"`
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ package store
|
|||||||
|
|
||||||
// A simple key value store that can either just be single instance in memory or a redis server (for now)
|
// A simple key value store that can either just be single instance in memory or a redis server (for now)
|
||||||
|
|
||||||
type Store[Value any] interface {
|
type KeyValueStore[Value any] interface {
|
||||||
Create(key string, value Value) error
|
Create(key string, value Value) error
|
||||||
Get(key string) (Value, error)
|
Get(key string) (Value, error)
|
||||||
Update(key string, session Value) error
|
Update(key string, session Value) error
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ func NewRedisStore[Value any]() *RedisStore[Value] {
|
|||||||
store := &RedisStore[Value]{
|
store := &RedisStore[Value]{
|
||||||
client: rdb,
|
client: rdb,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
prefix: "random_string_1",
|
||||||
}
|
}
|
||||||
return store
|
return store
|
||||||
}
|
}
|
||||||
@@ -77,7 +78,7 @@ func (m *RedisStore[Value]) Get(sessionID string) (Value, error) {
|
|||||||
return session_data, ErrKeyBackend
|
return session_data, ErrKeyBackend
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.Unmarshal(data, session_data); err != nil {
|
if err := json.Unmarshal(data, &session_data); err != nil {
|
||||||
logging.Error(err.Error())
|
logging.Error(err.Error())
|
||||||
return session_data, ErrKeyBackend
|
return session_data, ErrKeyBackend
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user