-
Notifications
You must be signed in to change notification settings - Fork 0
/
stores.go
67 lines (54 loc) · 1.64 KB
/
stores.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package stores
import (
"context"
"sync"
"github.com/lugondev/wallet-signer-manager/src/vaults"
"github.com/lugondev/wallet-signer-manager/pkg/errors"
"github.com/lugondev/wallet-signer-manager/src/stores/entities"
"github.com/lugondev/wallet-signer-manager/src/auth"
"github.com/lugondev/wallet-signer-manager/src/infra/log"
"github.com/lugondev/wallet-signer-manager/src/stores"
"github.com/lugondev/wallet-signer-manager/src/stores/database"
)
type Connector struct {
logger log.Logger
mux sync.RWMutex
roles auth.Roles
stores map[string]*entities.Store
vaults vaults.Vaults
db database.Database
}
var _ stores.Stores = &Connector{}
func NewConnector(roles auth.Roles, db database.Database, vaultsService vaults.Vaults, logger log.Logger) *Connector {
return &Connector{
logger: logger,
mux: sync.RWMutex{},
roles: roles,
stores: make(map[string]*entities.Store),
vaults: vaultsService,
db: db,
}
}
func (c *Connector) createStore(name, storeType string, store interface{}, allowedTenants []string) {
c.mux.Lock()
defer c.mux.Unlock()
c.stores[name] = &entities.Store{
Name: name,
AllowedTenants: allowedTenants,
Store: store,
StoreType: storeType,
}
}
func (c *Connector) getStore(_ context.Context, name string, resolver auth.Authorizator) (*entities.Store, error) {
c.mux.RLock()
defer c.mux.RUnlock()
if store, ok := c.stores[name]; ok {
if err := resolver.CheckAccess(store.AllowedTenants); err != nil {
return nil, err
}
return store, nil
}
errMessage := "store was not found"
c.logger.Error(errMessage, "name", name)
return nil, errors.NotFoundError(errMessage)
}