From 00586baffc320f5f32bd85d9a11aa0c527d211cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 14 Mar 2021 13:07:03 +0100 Subject: [PATCH] member providers extracted config. --- internal/member/file/manager.go | 21 ++++++++------------- internal/member/file/types.go | 14 ++++++++++++++ internal/member/manager.go | 9 +++++++-- internal/member/object/manager.go | 11 ++++------- internal/member/object/types.go | 15 +++++++++++++++ 5 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 internal/member/file/types.go create mode 100644 internal/member/object/types.go diff --git a/internal/member/file/manager.go b/internal/member/file/manager.go index c570b676..662203f5 100644 --- a/internal/member/file/manager.go +++ b/internal/member/file/manager.go @@ -10,21 +10,16 @@ import ( "demodesk/neko/internal/types" ) -func New(file string) types.MemberManager { +func New(config Config) types.MemberManager { return &MemberManagerCtx{ - file: file, - mu: sync.Mutex{}, + config: config, + mu: sync.Mutex{}, } } type MemberManagerCtx struct { - file string - mu sync.Mutex -} - -type MemberEntry struct { - Password string `json:"password"` - Profile types.MemberProfile `json:"profile"` + config Config + mu sync.Mutex } func (manager *MemberManagerCtx) Connect() error { @@ -146,7 +141,7 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty i := 0 for id, entry := range entries { - if i < offset || i > offset + limit { + if i < offset || i > offset+limit { continue } @@ -177,7 +172,7 @@ func (manager *MemberManagerCtx) Delete(id string) error { } func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, error) { - file, err := os.OpenFile(manager.file, os.O_RDONLY|os.O_CREATE, os.ModePerm) + file, err := os.OpenFile(manager.config.File, os.O_RDONLY|os.O_CREATE, os.ModePerm) if err != nil { return nil, err } @@ -219,5 +214,5 @@ func (manager *MemberManagerCtx) serialize(data map[string]MemberEntry) error { return err } - return ioutil.WriteFile(manager.file, raw, os.ModePerm) + return ioutil.WriteFile(manager.config.File, raw, os.ModePerm) } diff --git a/internal/member/file/types.go b/internal/member/file/types.go new file mode 100644 index 00000000..4a79b652 --- /dev/null +++ b/internal/member/file/types.go @@ -0,0 +1,14 @@ +package file + +import ( + "demodesk/neko/internal/types" +) + +type MemberEntry struct { + Password string `json:"password"` + Profile types.MemberProfile `json:"profile"` +} + +type Config struct { + File string +} diff --git a/internal/member/manager.go b/internal/member/manager.go index 0722dc0d..b1aa012e 100644 --- a/internal/member/manager.go +++ b/internal/member/manager.go @@ -11,9 +11,14 @@ import ( func New(config *config.Member) types.MemberManager { switch config.Provider { case "file": - return file.New(config.FilePath) + return file.New(file.Config{ + File: config.FilePath, + }) case "object": - return object.New() + return object.New(object.Config{ + AdminPassword: config.AdminPassword, + UserPassword: config.Password, + }) case "dummy": return dummy.New() } diff --git a/internal/member/object/manager.go b/internal/member/object/manager.go index ed2c148f..c18330d9 100644 --- a/internal/member/object/manager.go +++ b/internal/member/object/manager.go @@ -7,23 +7,20 @@ import ( "demodesk/neko/internal/types" ) -func New() types.MemberManager { +func New(config Config) types.MemberManager { return &MemberManagerCtx{ + config: config, entries: make(map[string]MemberEntry), mu: sync.Mutex{}, } } type MemberManagerCtx struct { + config Config entries map[string]MemberEntry mu sync.Mutex } -type MemberEntry struct { - Password string `json:"password"` - Profile types.MemberProfile `json:"profile"` -} - func (manager *MemberManagerCtx) Connect() error { return nil } @@ -123,7 +120,7 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty i := 0 for id, entry := range manager.entries { - if i < offset || i > offset + limit { + if i < offset || i > offset+limit { continue } diff --git a/internal/member/object/types.go b/internal/member/object/types.go new file mode 100644 index 00000000..e3b1b719 --- /dev/null +++ b/internal/member/object/types.go @@ -0,0 +1,15 @@ +package object + +import ( + "demodesk/neko/internal/types" +) + +type MemberEntry struct { + Password string `json:"password"` + Profile types.MemberProfile `json:"profile"` +} + +type Config struct { + AdminPassword string + UserPassword string +}