session.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package session_manager
  2. import (
  3. "sync"
  4. "github.com/google/uuid"
  5. )
  6. var (
  7. sessions map[string]*Session = map[string]*Session{}
  8. session_lock sync.RWMutex
  9. )
  10. type Session struct {
  11. id string
  12. tenant_id string
  13. user_id string
  14. plugin_identity string
  15. }
  16. func NewSession(tenant_id string, user_id string, plugin_identity string) *Session {
  17. s := &Session{
  18. id: uuid.New().String(),
  19. tenant_id: tenant_id,
  20. user_id: user_id,
  21. plugin_identity: plugin_identity,
  22. }
  23. session_lock.Lock()
  24. defer session_lock.Unlock()
  25. sessions[s.id] = s
  26. return s
  27. }
  28. func GetSession(id string) *Session {
  29. session_lock.RLock()
  30. defer session_lock.RUnlock()
  31. return sessions[id]
  32. }
  33. func DeleteSession(id string) {
  34. session_lock.Lock()
  35. defer session_lock.Unlock()
  36. delete(sessions, id)
  37. }
  38. func (s *Session) Close() {
  39. session_lock.Lock()
  40. defer session_lock.Unlock()
  41. delete(sessions, s.id)
  42. }
  43. func (s *Session) ID() string {
  44. return s.id
  45. }
  46. func (s *Session) TenantID() string {
  47. return s.tenant_id
  48. }
  49. func (s *Session) UserID() string {
  50. return s.user_id
  51. }
  52. func (s *Session) PluginIdentity() string {
  53. return s.plugin_identity
  54. }