add members database adapters interface.

This commit is contained in:
Miroslav Šedivý 2020-12-06 00:21:09 +01:00
parent edb9fef097
commit 100544198b
5 changed files with 116 additions and 58 deletions

View File

@ -0,0 +1,27 @@
package dummy
import (
"demodesk/neko/internal/types"
)
func New() types.MembersDatabase {
return &MembersDatabaseCtx{}
}
type MembersDatabaseCtx struct {}
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error {
return nil
}
func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile) error {
return nil
}
func (manager *MembersDatabaseCtx) Delete(id string) error {
return nil
}
func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile {
return map[string]types.MemberProfile{}
}

View File

@ -1,66 +1,22 @@
package database package database
import ( import (
"fmt" "demodesk/neko/internal/session/database/dummy"
"sync" "demodesk/neko/internal/session/database/object"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
"demodesk/neko/internal/config"
) )
func New() *MembersDatabaseCtx { func New(config *config.Session) types.MembersDatabase {
return &MembersDatabaseCtx{ // TODO: Load from config.
profiles: make(map[string]types.MemberProfile), adapter := "object"
mu: sync.Mutex{},
}
}
type MembersDatabaseCtx struct { switch adapter {
profiles map[string]types.MemberProfile case "object":
mu sync.Mutex return object.New()
} case "dummy":
return dummy.New()
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.profiles[id]
if ok {
return fmt.Errorf("Member ID already exists.")
} }
manager.profiles[id] = profile return dummy.New()
return nil
}
func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.profiles[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
manager.profiles[id] = profile
return nil
}
func (manager *MembersDatabaseCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.profiles[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
delete(manager.profiles, id)
return nil
}
func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.profiles
} }

View File

@ -0,0 +1,66 @@
package object
import (
"fmt"
"sync"
"demodesk/neko/internal/types"
)
func New() types.MembersDatabase {
return &MembersDatabaseCtx{
profiles: make(map[string]types.MemberProfile),
mu: sync.Mutex{},
}
}
type MembersDatabaseCtx struct {
profiles map[string]types.MemberProfile
mu sync.Mutex
}
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.profiles[id]
if ok {
return fmt.Errorf("Member ID already exists.")
}
manager.profiles[id] = profile
return nil
}
func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.profiles[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
manager.profiles[id] = profile
return nil
}
func (manager *MembersDatabaseCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
_, ok := manager.profiles[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
}
delete(manager.profiles, id)
return nil
}
func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.profiles
}

View File

@ -20,7 +20,7 @@ func New(config *config.Session) *SessionManagerCtx {
host: nil, host: nil,
hostMu: sync.Mutex{}, hostMu: sync.Mutex{},
config: config, config: config,
database: database.New(), database: database.New(config),
members: make(map[string]*SessionCtx), members: make(map[string]*SessionCtx),
membersMu: sync.Mutex{}, membersMu: sync.Mutex{},
emmiter: events.New(), emmiter: events.New(),
@ -31,6 +31,8 @@ func New(config *config.Session) *SessionManagerCtx {
_ = manager.add(id, profile) _ = manager.add(id, profile)
} }
// TODO: Move to Database, or make `admin` as reserved user.
// create default admin account at startup // create default admin account at startup
_ = manager.add("admin", types.MemberProfile{ _ = manager.add("admin", types.MemberProfile{
Secret: config.AdminPassword, Secret: config.AdminPassword,
@ -53,7 +55,7 @@ type SessionManagerCtx struct {
host types.Session host types.Session
hostMu sync.Mutex hostMu sync.Mutex
config *config.Session config *config.Session
database *database.MembersDatabaseCtx database types.MembersDatabase
members map[string]*SessionCtx members map[string]*SessionCtx
membersMu sync.Mutex membersMu sync.Mutex
emmiter events.EventEmmiter emmiter events.EventEmmiter

View File

@ -14,6 +14,13 @@ type MemberProfile struct {
CanAccessClipboard bool CanAccessClipboard bool
} }
type MembersDatabase interface {
Insert(id string, profile MemberProfile) error
Update(id string, profile MemberProfile) error
Delete(id string) error
Select() map[string]MemberProfile
}
type Session interface { type Session interface {
ID() string ID() string