member providers extracted config.

This commit is contained in:
Miroslav Šedivý 2021-03-14 13:07:03 +01:00
parent fceae3818b
commit 00586baffc
5 changed files with 48 additions and 22 deletions

View File

@ -10,21 +10,16 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New(file string) types.MemberManager { func New(config Config) types.MemberManager {
return &MemberManagerCtx{ return &MemberManagerCtx{
file: file, config: config,
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MemberManagerCtx struct { type MemberManagerCtx struct {
file string config Config
mu sync.Mutex mu sync.Mutex
}
type MemberEntry struct {
Password string `json:"password"`
Profile types.MemberProfile `json:"profile"`
} }
func (manager *MemberManagerCtx) Connect() error { func (manager *MemberManagerCtx) Connect() error {
@ -146,7 +141,7 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
i := 0 i := 0
for id, entry := range entries { for id, entry := range entries {
if i < offset || i > offset + limit { if i < offset || i > offset+limit {
continue continue
} }
@ -177,7 +172,7 @@ func (manager *MemberManagerCtx) Delete(id string) error {
} }
func (manager *MemberManagerCtx) deserialize() (map[string]MemberEntry, 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 { if err != nil {
return nil, err return nil, err
} }
@ -219,5 +214,5 @@ func (manager *MemberManagerCtx) serialize(data map[string]MemberEntry) error {
return err return err
} }
return ioutil.WriteFile(manager.file, raw, os.ModePerm) return ioutil.WriteFile(manager.config.File, raw, os.ModePerm)
} }

View File

@ -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
}

View File

@ -11,9 +11,14 @@ import (
func New(config *config.Member) types.MemberManager { func New(config *config.Member) types.MemberManager {
switch config.Provider { switch config.Provider {
case "file": case "file":
return file.New(config.FilePath) return file.New(file.Config{
File: config.FilePath,
})
case "object": case "object":
return object.New() return object.New(object.Config{
AdminPassword: config.AdminPassword,
UserPassword: config.Password,
})
case "dummy": case "dummy":
return dummy.New() return dummy.New()
} }

View File

@ -7,23 +7,20 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
func New() types.MemberManager { func New(config Config) types.MemberManager {
return &MemberManagerCtx{ return &MemberManagerCtx{
config: config,
entries: make(map[string]MemberEntry), entries: make(map[string]MemberEntry),
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MemberManagerCtx struct { type MemberManagerCtx struct {
config Config
entries map[string]MemberEntry entries map[string]MemberEntry
mu sync.Mutex mu sync.Mutex
} }
type MemberEntry struct {
Password string `json:"password"`
Profile types.MemberProfile `json:"profile"`
}
func (manager *MemberManagerCtx) Connect() error { func (manager *MemberManagerCtx) Connect() error {
return nil return nil
} }
@ -123,7 +120,7 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
i := 0 i := 0
for id, entry := range manager.entries { for id, entry := range manager.entries {
if i < offset || i > offset + limit { if i < offset || i > offset+limit {
continue continue
} }

View File

@ -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
}