mobility-accounts/storage/storage.go

66 lines
1.3 KiB
Go

package storage
import (
"fmt"
"time"
"github.com/spf13/viper"
)
// Storage interface
type Storage struct {
DB DBStorage
KV KVStore
}
func NewStorage(cfg *viper.Viper) (Storage, error) {
dbstorage, err := NewDBStorage(cfg)
if err != nil {
return Storage{}, err
}
kvstore, err := NewKVStore(cfg)
if err != nil {
return Storage{}, err
}
return Storage{
DB: dbstorage,
KV: kvstore,
}, nil
}
type DBStorage interface {
GetAccount(id string) (*Account, error)
LocalAuthentication(namespace string, username string, email string, phone_number string) (*Account, error)
GetAccounts(namespaces []string) ([]Account, error)
GetAccountsByIds(accountids []string) ([]Account, error)
CreateAccount(account Account) error
UpdateAccount(account Account) error
}
func NewDBStorage(cfg *viper.Viper) (DBStorage, error) {
var (
storage_type = cfg.GetString("storage.db.type")
)
switch storage_type {
case "mongodb":
s, err := NewMongoDBStorage(cfg)
return s, err
default:
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
}
}
type KVStore interface {
Put(string, any) error
PutWithTTL(string, any, time.Duration) error
Get(string) (any, error)
Delete(string) error
}
func NewKVStore(cfg *viper.Viper) (KVStore, error) {
kv, err := NewEtcdKVStore(cfg)
return kv, err
}