added member provider.

This commit is contained in:
Miroslav Šedivý 2021-03-14 01:08:50 +01:00
parent 594404824f
commit 7c79b48750
10 changed files with 141 additions and 78 deletions

View File

@ -21,6 +21,7 @@ func init() {
neko.Service.Configs.Desktop, neko.Service.Configs.Desktop,
neko.Service.Configs.Capture, neko.Service.Configs.Capture,
neko.Service.Configs.WebRTC, neko.Service.Configs.WebRTC,
neko.Service.Configs.Member,
neko.Service.Configs.Session, neko.Service.Configs.Session,
neko.Service.Configs.Server, neko.Service.Configs.Server,
}, modules.Configs()...) }, modules.Configs()...)

44
internal/config/member.go Normal file
View File

@ -0,0 +1,44 @@
package config
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type Member struct {
Provider string
FilePath string
Password string
AdminPassword string
}
func (Member) Init(cmd *cobra.Command) error {
cmd.PersistentFlags().String("members_provider", "file", "choose members provider")
if err := viper.BindPFlag("members_provider", cmd.PersistentFlags().Lookup("members_provider")); err != nil {
return err
}
cmd.PersistentFlags().String("members_file_path", "/home/neko/members.json", "mebmer file provider path")
if err := viper.BindPFlag("members_file_path", cmd.PersistentFlags().Lookup("members_file_path")); err != nil {
return err
}
cmd.PersistentFlags().String("password", "neko", "password for connecting to stream")
if err := viper.BindPFlag("password", cmd.PersistentFlags().Lookup("password")); err != nil {
return err
}
cmd.PersistentFlags().String("password_admin", "admin", "admin password for connecting to stream")
if err := viper.BindPFlag("password_admin", cmd.PersistentFlags().Lookup("password_admin")); err != nil {
return err
}
return nil
}
func (s *Member) Set() {
s.Provider = viper.GetString("members_provider")
s.FilePath = viper.GetString("members_file_path")
s.Password = viper.GetString("password")
s.AdminPassword = viper.GetString("password_admin")
}

View File

@ -1,35 +0,0 @@
package dummy
import (
"demodesk/neko/internal/types"
)
func New() types.MembersDatabase {
return &MembersDatabaseCtx{}
}
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
}
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, error) {
return map[string]types.MemberProfile{}, nil
}

View File

@ -1,22 +0,0 @@
package database
import (
"demodesk/neko/internal/config"
"demodesk/neko/internal/session/database/dummy"
"demodesk/neko/internal/session/database/file"
"demodesk/neko/internal/session/database/object"
"demodesk/neko/internal/types"
)
func New(config *config.Session) types.MembersDatabase {
switch config.DatabaseAdapter {
case "file":
return file.New(config.FilePath)
case "object":
return object.New()
case "dummy":
return dummy.New()
}
return dummy.New()
}

View File

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

View File

@ -10,27 +10,27 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New(file string) types.MembersDatabase { func New(file string) types.MemberManager {
return &MembersDatabaseCtx{ return &MemberManagerCtx{
file: file, file: file,
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MembersDatabaseCtx struct { type MemberManagerCtx struct {
file string file string
mu sync.Mutex mu sync.Mutex
} }
func (manager *MembersDatabaseCtx) Connect() error { func (manager *MemberManagerCtx) Connect() error {
return nil return nil
} }
func (manager *MembersDatabaseCtx) Disconnect() error { func (manager *MemberManagerCtx) Disconnect() error {
return nil return nil
} }
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) Insert(id string, profile types.MemberProfile) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -49,7 +49,7 @@ func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile
return manager.serialize(profiles) return manager.serialize(profiles)
} }
func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -68,7 +68,7 @@ func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile
return manager.serialize(profiles) return manager.serialize(profiles)
} }
func (manager *MembersDatabaseCtx) Delete(id string) error { func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -87,7 +87,7 @@ func (manager *MembersDatabaseCtx) Delete(id string) error {
return manager.serialize(profiles) return manager.serialize(profiles)
} }
func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, error) { func (manager *MemberManagerCtx) Select() (map[string]types.MemberProfile, error) {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -95,7 +95,7 @@ func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, err
return profiles, err return profiles, err
} }
func (manager *MembersDatabaseCtx) deserialize() (map[string]types.MemberProfile, error) { func (manager *MemberManagerCtx) deserialize() (map[string]types.MemberProfile, error) {
file, err := os.OpenFile(manager.file, os.O_RDONLY|os.O_CREATE, os.ModePerm) file, err := os.OpenFile(manager.file, os.O_RDONLY|os.O_CREATE, os.ModePerm)
if err != nil { if err != nil {
return nil, err return nil, err
@ -118,7 +118,7 @@ func (manager *MembersDatabaseCtx) deserialize() (map[string]types.MemberProfile
return profiles, nil return profiles, nil
} }
func (manager *MembersDatabaseCtx) serialize(data map[string]types.MemberProfile) error { func (manager *MemberManagerCtx) serialize(data map[string]types.MemberProfile) error {
raw, err := json.Marshal(data) raw, err := json.Marshal(data)
if err != nil { if err != nil {
return err return err

View File

@ -0,0 +1,22 @@
package member
import (
"demodesk/neko/internal/config"
"demodesk/neko/internal/member/dummy"
"demodesk/neko/internal/member/file"
"demodesk/neko/internal/member/object"
"demodesk/neko/internal/types"
)
func New(config *config.Member) types.MemberManager {
switch config.Provider {
case "file":
return file.New(config.FilePath)
case "object":
return object.New()
case "dummy":
return dummy.New()
}
return dummy.New()
}

View File

@ -7,27 +7,27 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New() types.MembersDatabase { func New() types.MemberManager {
return &MembersDatabaseCtx{ return &MemberManagerCtx{
profiles: make(map[string]types.MemberProfile), profiles: make(map[string]types.MemberProfile),
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MembersDatabaseCtx struct { type MemberManagerCtx struct {
profiles map[string]types.MemberProfile profiles map[string]types.MemberProfile
mu sync.Mutex mu sync.Mutex
} }
func (manager *MembersDatabaseCtx) Connect() error { func (manager *MemberManagerCtx) Connect() error {
return nil return nil
} }
func (manager *MembersDatabaseCtx) Disconnect() error { func (manager *MemberManagerCtx) Disconnect() error {
return nil return nil
} }
func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) Insert(id string, profile types.MemberProfile) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -40,7 +40,7 @@ func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile
return nil return nil
} }
func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -53,7 +53,7 @@ func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile
return nil return nil
} }
func (manager *MembersDatabaseCtx) Delete(id string) error { func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -66,7 +66,7 @@ func (manager *MembersDatabaseCtx) Delete(id string) error {
return nil return nil
} }
func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, error) { func (manager *MemberManagerCtx) Select() (map[string]types.MemberProfile, error) {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()

View File

@ -1,6 +1,6 @@
package types package types
type MembersDatabase interface { type MemberManager interface {
Connect() error Connect() error
Disconnect() error Disconnect() error

18
neko.go
View File

@ -11,7 +11,9 @@ import (
"demodesk/neko/internal/config" "demodesk/neko/internal/config"
"demodesk/neko/internal/desktop" "demodesk/neko/internal/desktop"
"demodesk/neko/internal/http" "demodesk/neko/internal/http"
"demodesk/neko/internal/member"
"demodesk/neko/internal/session" "demodesk/neko/internal/session"
"demodesk/neko/internal/types"
"demodesk/neko/internal/webrtc" "demodesk/neko/internal/webrtc"
"demodesk/neko/internal/websocket" "demodesk/neko/internal/websocket"
"demodesk/neko/modules" "demodesk/neko/modules"
@ -66,6 +68,7 @@ func init() {
Desktop: &config.Desktop{}, Desktop: &config.Desktop{},
Capture: &config.Capture{}, Capture: &config.Capture{},
WebRTC: &config.WebRTC{}, WebRTC: &config.WebRTC{},
Member: &config.Member{},
Session: &config.Session{}, Session: &config.Session{},
Server: &config.Server{}, Server: &config.Server{},
}, },
@ -106,6 +109,7 @@ type Configs struct {
Desktop *config.Desktop Desktop *config.Desktop
Capture *config.Capture Capture *config.Capture
WebRTC *config.WebRTC WebRTC *config.WebRTC
Member *config.Member
Session *config.Session Session *config.Session
Server *config.Server Server *config.Server
} }
@ -118,6 +122,7 @@ type Neko struct {
desktopManager *desktop.DesktopManagerCtx desktopManager *desktop.DesktopManagerCtx
captureManager *capture.CaptureManagerCtx captureManager *capture.CaptureManagerCtx
webRTCManager *webrtc.WebRTCManagerCtx webRTCManager *webrtc.WebRTCManagerCtx
memberManager types.MemberManager
sessionManager *session.SessionManagerCtx sessionManager *session.SessionManagerCtx
webSocketManager *websocket.WebSocketManagerCtx webSocketManager *websocket.WebSocketManagerCtx
apiManager *api.ApiManagerCtx apiManager *api.ApiManagerCtx
@ -129,6 +134,13 @@ func (neko *Neko) Preflight() {
} }
func (neko *Neko) Start() { func (neko *Neko) Start() {
neko.memberManager = member.New(
neko.Configs.Member,
)
if err := neko.memberManager.Connect(); err != nil {
neko.logger.Panic().Err(err).Msg("unable to connect to member manager")
}
neko.sessionManager = session.New( neko.sessionManager = session.New(
neko.Configs.Session, neko.Configs.Session,
) )
@ -181,6 +193,12 @@ func (neko *Neko) Start() {
} }
func (neko *Neko) Shutdown() { func (neko *Neko) Shutdown() {
if err := neko.memberManager.Disconnect(); err != nil {
neko.logger.Err(err).Msg("member manager disconnect with an error")
} else {
neko.logger.Debug().Msg("member manager disconnect")
}
if err := neko.desktopManager.Shutdown(); err != nil { if err := neko.desktopManager.Shutdown(); err != nil {
neko.logger.Err(err).Msg("desktop manager shutdown with an error") neko.logger.Err(err).Msg("desktop manager shutdown with an error")
} else { } else {