add connect / disconnect to adapters.

This commit is contained in:
Miroslav Šedivý 2020-12-06 18:11:11 +01:00
parent 6ca1e27f01
commit 35cd0f5270
6 changed files with 87 additions and 42 deletions

View File

@ -10,6 +10,14 @@ func New() types.MembersDatabase {
type MembersDatabaseCtx struct {}
func (manager *MembersDatabaseCtx) Connect() error {
return nil
}
func (manager *MembersDatabaseCtx) Disconnect() error {
return nil
}
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error {
return nil
}
@ -22,6 +30,6 @@ func (manager *MembersDatabaseCtx) Delete(id string) error {
return nil
}
func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile {
return map[string]types.MemberProfile{}
func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, error) {
return map[string]types.MemberProfile{}, nil
}

View File

@ -7,8 +7,6 @@ import (
"fmt"
"sync"
"github.com/rs/zerolog/log"
"demodesk/neko/internal/types"
)
@ -24,6 +22,14 @@ type MembersDatabaseCtx struct {
mu sync.Mutex
}
func (manager *MembersDatabaseCtx) Connect() error {
return nil
}
func (manager *MembersDatabaseCtx) Disconnect() error {
return nil
}
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
@ -81,17 +87,12 @@ func (manager *MembersDatabaseCtx) Delete(id string) error {
return manager.serialize(profiles)
}
func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile {
func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
profiles, err := manager.deserialize()
if err != nil {
// TODO: Refactor.
log.Panic().Err(err).Msg("could not read members file")
}
return profiles
return profiles, err
}
func (manager *MembersDatabaseCtx) deserialize() (map[string]types.MemberProfile, error) {

View File

@ -19,6 +19,14 @@ type MembersDatabaseCtx struct {
mu sync.Mutex
}
func (manager *MembersDatabaseCtx) Connect() error {
return nil
}
func (manager *MembersDatabaseCtx) Disconnect() error {
return nil
}
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
@ -58,9 +66,9 @@ func (manager *MembersDatabaseCtx) Delete(id string) error {
return nil
}
func (manager *MembersDatabaseCtx) Select() map[string]types.MemberProfile {
func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.profiles
return manager.profiles, nil
}

View File

@ -15,7 +15,7 @@ import (
)
func New(config *config.Session) *SessionManagerCtx {
manager := &SessionManagerCtx{
return &SessionManagerCtx{
logger: log.With().Str("module", "session").Logger(),
host: nil,
hostMu: sync.Mutex{},
@ -25,29 +25,6 @@ func New(config *config.Session) *SessionManagerCtx {
membersMu: sync.Mutex{},
emmiter: events.New(),
}
// import database users
for id, profile := range manager.database.Select() {
_ = 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,
Name: "Administrator",
IsAdmin: true,
})
// create default user account at startup
_ = manager.add("user", types.MemberProfile{
Secret: config.Password,
Name: "User",
IsAdmin: false,
})
return manager
}
type SessionManagerCtx struct {
@ -77,6 +54,42 @@ func (manager *SessionManagerCtx) add(id string, profile types.MemberProfile) ty
// ---
// members
// ---
func (manager *SessionManagerCtx) Connect() error {
if err := manager.database.Connect(); err != nil {
return err
}
profiles, err := manager.database.Select()
if err != nil {
return err
}
for id, profile := range profiles {
_ = 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: manager.config.AdminPassword,
Name: "Administrator",
IsAdmin: true,
})
// create default user account at startup
_ = manager.add("user", types.MemberProfile{
Secret: manager.config.Password,
Name: "User",
IsAdmin: false,
})
return nil
}
func (manager *SessionManagerCtx) Disconnect() error {
return manager.database.Disconnect()
}
func (manager *SessionManagerCtx) Create(id string, profile types.MemberProfile) (types.Session, error) {
manager.membersMu.Lock()

View File

@ -15,10 +15,13 @@ type MemberProfile struct {
}
type MembersDatabase interface {
Connect() error
Disconnect() error
Insert(id string, profile MemberProfile) error
Update(id string, profile MemberProfile) error
Delete(id string) error
Select() map[string]MemberProfile
Select() (map[string]MemberProfile, error)
}
type Session interface {
@ -48,6 +51,9 @@ type Session interface {
}
type SessionManager interface {
Connect() error
Disconnect() error
Create(id string, profile MemberProfile) (Session, error)
Update(id string, profile MemberProfile) error
Get(id string) (Session, bool)

17
neko.go
View File

@ -128,6 +128,13 @@ func (neko *Neko) Preflight() {
}
func (neko *Neko) Start() {
neko.sessionManager = session.New(
neko.Configs.Session,
)
if err := neko.sessionManager.Connect(); err != nil {
neko.logger.Panic().Err(err).Msg("unable to connect to session manager")
}
neko.desktopManager = desktop.New(
neko.Configs.Capture.Display,
neko.Configs.Desktop,
@ -147,10 +154,6 @@ func (neko *Neko) Start() {
)
neko.webRTCManager.Start()
neko.sessionManager = session.New(
neko.Configs.Session,
)
neko.webSocketManager = websocket.New(
neko.sessionManager,
neko.desktopManager,
@ -175,6 +178,12 @@ func (neko *Neko) Start() {
}
func (neko *Neko) Shutdown() {
if err := neko.sessionManager.Disconnect(); err != nil {
neko.logger.Err(err).Msg("session manager disconnect with an error")
} else {
neko.logger.Debug().Msg("session manager disconnect")
}
if err := neko.desktopManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("desktop manager shutdown with an error")
} else {