diff --git a/cmd/serve.go b/cmd/serve.go index 95ab608b..d1cc3c6e 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -21,6 +21,7 @@ func init() { neko.Service.Configs.Desktop, neko.Service.Configs.Capture, neko.Service.Configs.WebRTC, + neko.Service.Configs.Member, neko.Service.Configs.Session, neko.Service.Configs.Server, }, modules.Configs()...) diff --git a/internal/config/member.go b/internal/config/member.go new file mode 100644 index 00000000..8fe03cb8 --- /dev/null +++ b/internal/config/member.go @@ -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") +} diff --git a/internal/member/database/dummy/adapter.go b/internal/member/database/dummy/adapter.go deleted file mode 100644 index 5155d213..00000000 --- a/internal/member/database/dummy/adapter.go +++ /dev/null @@ -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 -} diff --git a/internal/member/database/manager.go b/internal/member/database/manager.go deleted file mode 100644 index a1c7bc5a..00000000 --- a/internal/member/database/manager.go +++ /dev/null @@ -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() -} diff --git a/internal/member/dummy/adapter.go b/internal/member/dummy/adapter.go new file mode 100644 index 00000000..b4888048 --- /dev/null +++ b/internal/member/dummy/adapter.go @@ -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 +} diff --git a/internal/member/database/file/adapter.go b/internal/member/file/adapter.go similarity index 69% rename from internal/member/database/file/adapter.go rename to internal/member/file/adapter.go index 0508d4ed..6218f445 100644 --- a/internal/member/database/file/adapter.go +++ b/internal/member/file/adapter.go @@ -10,27 +10,27 @@ import ( "demodesk/neko/internal/types" ) -func New(file string) types.MembersDatabase { - return &MembersDatabaseCtx{ +func New(file string) types.MemberManager { + return &MemberManagerCtx{ file: file, mu: sync.Mutex{}, } } -type MembersDatabaseCtx struct { +type MemberManagerCtx struct { file string mu sync.Mutex } -func (manager *MembersDatabaseCtx) Connect() error { +func (manager *MemberManagerCtx) Connect() error { return nil } -func (manager *MembersDatabaseCtx) Disconnect() error { +func (manager *MemberManagerCtx) Disconnect() error { 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() defer manager.mu.Unlock() @@ -49,7 +49,7 @@ func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile 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() defer manager.mu.Unlock() @@ -68,7 +68,7 @@ func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile return manager.serialize(profiles) } -func (manager *MembersDatabaseCtx) Delete(id string) error { +func (manager *MemberManagerCtx) Delete(id string) error { manager.mu.Lock() defer manager.mu.Unlock() @@ -87,7 +87,7 @@ func (manager *MembersDatabaseCtx) Delete(id string) error { 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() defer manager.mu.Unlock() @@ -95,7 +95,7 @@ func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, 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) if err != nil { return nil, err @@ -118,7 +118,7 @@ func (manager *MembersDatabaseCtx) deserialize() (map[string]types.MemberProfile 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) if err != nil { return err diff --git a/internal/member/manager.go b/internal/member/manager.go new file mode 100644 index 00000000..0722dc0d --- /dev/null +++ b/internal/member/manager.go @@ -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() +} diff --git a/internal/member/database/object/adapter.go b/internal/member/object/adapter.go similarity index 62% rename from internal/member/database/object/adapter.go rename to internal/member/object/adapter.go index 18c05c32..412ea012 100644 --- a/internal/member/database/object/adapter.go +++ b/internal/member/object/adapter.go @@ -7,27 +7,27 @@ import ( "demodesk/neko/internal/types" ) -func New() types.MembersDatabase { - return &MembersDatabaseCtx{ +func New() types.MemberManager { + return &MemberManagerCtx{ profiles: make(map[string]types.MemberProfile), mu: sync.Mutex{}, } } -type MembersDatabaseCtx struct { +type MemberManagerCtx struct { profiles map[string]types.MemberProfile mu sync.Mutex } -func (manager *MembersDatabaseCtx) Connect() error { +func (manager *MemberManagerCtx) Connect() error { return nil } -func (manager *MembersDatabaseCtx) Disconnect() error { +func (manager *MemberManagerCtx) Disconnect() error { 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() defer manager.mu.Unlock() @@ -40,7 +40,7 @@ func (manager *MembersDatabaseCtx) Insert(id string, profile types.MemberProfile 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() defer manager.mu.Unlock() @@ -53,7 +53,7 @@ func (manager *MembersDatabaseCtx) Update(id string, profile types.MemberProfile return nil } -func (manager *MembersDatabaseCtx) Delete(id string) error { +func (manager *MemberManagerCtx) Delete(id string) error { manager.mu.Lock() defer manager.mu.Unlock() @@ -66,7 +66,7 @@ func (manager *MembersDatabaseCtx) Delete(id string) error { return nil } -func (manager *MembersDatabaseCtx) Select() (map[string]types.MemberProfile, error) { +func (manager *MemberManagerCtx) Select() (map[string]types.MemberProfile, error) { manager.mu.Lock() defer manager.mu.Unlock() diff --git a/internal/types/member.go b/internal/types/member.go index 403d6c54..90e4e196 100644 --- a/internal/types/member.go +++ b/internal/types/member.go @@ -1,6 +1,6 @@ package types -type MembersDatabase interface { +type MemberManager interface { Connect() error Disconnect() error diff --git a/neko.go b/neko.go index 7b912471..1dd41749 100644 --- a/neko.go +++ b/neko.go @@ -11,7 +11,9 @@ import ( "demodesk/neko/internal/config" "demodesk/neko/internal/desktop" "demodesk/neko/internal/http" + "demodesk/neko/internal/member" "demodesk/neko/internal/session" + "demodesk/neko/internal/types" "demodesk/neko/internal/webrtc" "demodesk/neko/internal/websocket" "demodesk/neko/modules" @@ -66,6 +68,7 @@ func init() { Desktop: &config.Desktop{}, Capture: &config.Capture{}, WebRTC: &config.WebRTC{}, + Member: &config.Member{}, Session: &config.Session{}, Server: &config.Server{}, }, @@ -106,6 +109,7 @@ type Configs struct { Desktop *config.Desktop Capture *config.Capture WebRTC *config.WebRTC + Member *config.Member Session *config.Session Server *config.Server } @@ -118,6 +122,7 @@ type Neko struct { desktopManager *desktop.DesktopManagerCtx captureManager *capture.CaptureManagerCtx webRTCManager *webrtc.WebRTCManagerCtx + memberManager types.MemberManager sessionManager *session.SessionManagerCtx webSocketManager *websocket.WebSocketManagerCtx apiManager *api.ApiManagerCtx @@ -129,6 +134,13 @@ func (neko *Neko) Preflight() { } 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.Configs.Session, ) @@ -181,6 +193,12 @@ func (neko *Neko) Start() { } 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 { neko.logger.Err(err).Msg("desktop manager shutdown with an error") } else {