make session manager a singleton

This commit is contained in:
2026-04-03 14:45:34 -04:00
parent 46db63e62a
commit d1992ec466

View File

@@ -2,6 +2,7 @@ package session
import ( import (
"net/http" "net/http"
"sync"
"time" "time"
"astraltech.xyz/accountmanager/src/logging" "astraltech.xyz/accountmanager/src/logging"
@@ -13,6 +14,9 @@ type SessionManager struct {
store SessionStore store SessionStore
} }
var instance *SessionManager
var once sync.Once
type StoreType int type StoreType int
const ( const (
@@ -20,15 +24,17 @@ const (
) )
func CreateSessionManager(storeType StoreType) *SessionManager { func CreateSessionManager(storeType StoreType) *SessionManager {
sessionManager := SessionManager{} once.Do(func() {
switch storeType { instance = &SessionManager{}
case InMemory: switch storeType {
{ case InMemory:
sessionManager.store = NewMemoryStore() {
break instance.store = NewMemoryStore()
break
}
} }
} })
return &sessionManager return instance
} }
func (manager *SessionManager) CreateSession(userID string) (cookie *http.Cookie, err error) { func (manager *SessionManager) CreateSession(userID string) (cookie *http.Cookie, err error) {