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
import (
"fmt"
"sync"
"demodesk/neko/internal/session/database/dummy"
"demodesk/neko/internal/session/database/object"
"demodesk/neko/internal/types"
"demodesk/neko/internal/config"
)
func New() *MembersDatabaseCtx {
return &MembersDatabaseCtx{
profiles: make(map[string]types.MemberProfile),
mu: sync.Mutex{},
}
func New(config *config.Session) types.MembersDatabase {
// TODO: Load from config.
adapter := "object"
switch adapter {
case "object":
return object.New()
case "dummy":
return dummy.New()
}
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
return dummy.New()
}

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

View File

@ -14,6 +14,13 @@ type MemberProfile struct {
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 {
ID() string