Draft example of dev-0.1.0 refactor #34
225
cmd/main.go
225
cmd/main.go
@ -10,152 +10,159 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/cache"
|
|
||||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||||
|
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
|
||||||
"git.defalsify.org/vise.git/engine"
|
"git.defalsify.org/vise.git/engine"
|
||||||
"git.defalsify.org/vise.git/persist"
|
"git.defalsify.org/vise.git/persist"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
scriptDir = path.Join("services", "registration")
|
logg = logging.NewVanilla()
|
||||||
store = fsdb.NewFsDb()
|
flags *FlagParser
|
||||||
pr = persist.NewPersister(store)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type menuResource struct {
|
|
||||||
*resource.DbResource
|
func getFlags(fp string, debug bool) error {
|
||||||
|
Flags = NewFlagParser().WithDebug()
|
||||||
|
flags, err := Flags.Load(fp)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func newMenuResource(rs *resource.DbResource) resource.Resource {
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||||
return &menuResource{
|
ussdHandlers, err := ussd.NewHandlers(appFlags pr.GetState(), dataStore)
|
||||||
rs,
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
rs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
|
||||||
|
rs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
|
||||||
|
rs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
|
||||||
|
rs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin)
|
||||||
|
rs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier)
|
||||||
|
rs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus)
|
||||||
|
rs.AddLocalFunc("authorize_account", ussdHandlers.Authorize)
|
||||||
|
rs.AddLocalFunc("quit", ussdHandlers.Quit)
|
||||||
|
rs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance)
|
||||||
|
rs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient)
|
||||||
|
rs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset)
|
||||||
|
rs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount)
|
||||||
|
rs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount)
|
||||||
|
rs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount)
|
||||||
|
rs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient)
|
||||||
|
rs.AddLocalFunc("get_sender", ussdHandlers.GetSender)
|
||||||
|
rs.AddLocalFunc("get_amount", ussdHandlers.GetAmount)
|
||||||
|
rs.AddLocalFunc("reset_incorrect", ussdHandlers.ResetIncorrectPin)
|
||||||
|
rs.AddLocalFunc("save_firstname", ussdHandlers.SaveFirstname)
|
||||||
|
rs.AddLocalFunc("save_familyname", ussdHandlers.SaveFamilyname)
|
||||||
|
rs.AddLocalFunc("save_gender", ussdHandlers.SaveGender)
|
||||||
|
rs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
||||||
|
rs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
||||||
|
rs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
||||||
|
rs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
|
||||||
|
rs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
||||||
|
rs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
||||||
|
rs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
||||||
|
rs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob)
|
||||||
|
rs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob)
|
||||||
|
rs.AddLocalFunc("set_reset_single_edit", ussdHandlers.SetResetSingleEdit)
|
||||||
|
rs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction)
|
||||||
|
|
||||||
|
return ussdHandlers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPersister(dbDir string) (*persist.Persister, error) {
|
||||||
|
err = os.MkdirAll(dp, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "states.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
pr := persist.NewPersister(store)
|
||||||
|
|
||||||
|
return pr
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserdataDb(dbDir string) {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "userdata.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
func getResource(resourceDir string) (resource.Resource, error) {
|
||||||
|
store := fsdb.NewFsDb()
|
||||||
|
err = store.Connect(ctx, resourceDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rfs := resource.NewDbResource(store)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEngine(cfg Config, rs resource.Resource, pr *persister.Persister) {
|
||||||
|
|
||||||
|
en := engine.NewEngine(cfg, rfs)
|
||||||
|
en = en.WithPersister(pr)
|
||||||
|
return en
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var dir string
|
var dbDir string
|
||||||
|
var resourceDir string
|
||||||
var root string
|
var root string
|
||||||
var size uint
|
var size uint
|
||||||
var sessionId string
|
var sessionId string
|
||||||
flag.StringVar(&dir, "d", ".", "resource dir to read from")
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||||
flag.UintVar(&size, "s", 0, "max size of output")
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
||||||
|
flag.UintVar(&size, "s", 160, "max size of output")
|
||||||
flag.StringVar(&root, "root", "root", "entry point symbol")
|
flag.StringVar(&root, "root", "root", "entry point symbol")
|
||||||
flag.StringVar(&sessionId, "session-id", "default", "session id")
|
flag.StringVar(&sessionId, "session-id", "default", "session id")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
|
logg.Infof("starting session", "symbol", root, "dbdir", dbDir, "sessionid", sessionId, "outsize", size)
|
||||||
|
|
||||||
ctx := context.Background()
|
fl, err := getFlags()
|
||||||
|
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
|
||||||
file, err := os.Open(pfp)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to open CSV file: %v\n", err)
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
reader := csv.NewReader(file)
|
|
||||||
|
|
||||||
// Iterate through the CSV records and register the flags
|
|
||||||
for {
|
|
||||||
record, err := reader.Read()
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "Error reading CSV file: %v\n", err)
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the record starts with "flag" and has at least 3 columns
|
rs, err := getResource(resourceDir)
|
||||||
if len(record) < 3 || record[0] != "flag" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
flagName := record[1]
|
|
||||||
flagValue, err := strconv.Atoi(record[2])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to convert flag value %s to integer: %v\n", record[2], err)
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
continue
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register the flag
|
pr, err := getDataPersister(dbDir)
|
||||||
state.FlagDebugger.Register(uint32(flagValue), flagName)
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
store, err := getUserdataDb(dataDir)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
hn, err := getHandlers(fl, rs, pr, store)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
ca := cache.NewCache()
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: sym,
|
||||||
SessionId: sessionId,
|
SessionId: sessionId,
|
||||||
|
OutputSize: size,
|
||||||
FlagCount: uint32(16),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
en := getEngine(cfg, rs, pr)
|
||||||
dp := path.Join(scriptDir, ".state")
|
|
||||||
err = os.MkdirAll(dp, 0700)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "state dir create exited with error: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
store := fsdb.NewFsDb()
|
|
||||||
err = store.Connect(ctx, scriptDir)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rfs := resource.NewDbResource(store)
|
|
||||||
|
|
||||||
rs, ok := newMenuResource(rfs).(*menuResource)
|
|
||||||
if !ok {
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
en := engine.NewEngine(cfg, rs)
|
|
||||||
en = en.WithMemory(ca)
|
|
||||||
en = en.WithPersister(pr)
|
|
||||||
|
|
||||||
fp := path.Join(dp, sessionId)
|
|
||||||
|
|
||||||
ussdHandlers, err := ussd.NewHandlers(fp, pr.State, sessionId)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "handler setup failed with error: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
rfs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
|
|
||||||
rfs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
|
|
||||||
rfs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
|
|
||||||
rfs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin)
|
|
||||||
rfs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier)
|
|
||||||
rfs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus)
|
|
||||||
rfs.AddLocalFunc("authorize_account", ussdHandlers.Authorize)
|
|
||||||
rfs.AddLocalFunc("quit", ussdHandlers.Quit)
|
|
||||||
rfs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance)
|
|
||||||
rfs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient)
|
|
||||||
rfs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset)
|
|
||||||
rfs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount)
|
|
||||||
rfs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount)
|
|
||||||
rfs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount)
|
|
||||||
rfs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient)
|
|
||||||
rfs.AddLocalFunc("get_sender", ussdHandlers.GetSender)
|
|
||||||
rfs.AddLocalFunc("get_amount", ussdHandlers.GetAmount)
|
|
||||||
rfs.AddLocalFunc("reset_incorrect", ussdHandlers.ResetIncorrectPin)
|
|
||||||
rfs.AddLocalFunc("save_firstname", ussdHandlers.SaveFirstname)
|
|
||||||
rfs.AddLocalFunc("save_familyname", ussdHandlers.SaveFamilyname)
|
|
||||||
rfs.AddLocalFunc("save_gender", ussdHandlers.SaveGender)
|
|
||||||
rfs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
|
||||||
rfs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
|
||||||
rfs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
|
||||||
rfs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
|
|
||||||
rfs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
|
||||||
rfs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
|
||||||
rfs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
|
||||||
rfs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob)
|
|
||||||
rfs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob)
|
|
||||||
rfs.AddLocalFunc("set_reset_single_edit", ussdHandlers.SetResetSingleEdit)
|
|
||||||
rfs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction)
|
|
||||||
|
|
||||||
_, err = en.Init(ctx)
|
_, err = en.Init(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
|
||||||
|
2
go-vise
2
go-vise
@ -1 +1 @@
|
|||||||
Subproject commit 1f47a674d95380be8c387f410f0342eb72357df5
|
Subproject commit 326bdb5018ae81ee493371b56bd4564a8b35da59
|
@ -11,9 +11,13 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/asm"
|
"git.defalsify.org/vise.git/asm"
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
"git.defalsify.org/vise.git/lang"
|
"git.defalsify.org/vise.git/lang"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.defalsify.org/vise.git/state"
|
"git.defalsify.org/vise.git/state"
|
||||||
|
"git.defalsify.org/vise.git/cache"
|
||||||
|
"git.defalsify.org/vise.git/persist"
|
||||||
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||||
"github.com/graygnuorg/go-gdbm"
|
"github.com/graygnuorg/go-gdbm"
|
||||||
@ -21,6 +25,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
logg = logging.NewVanilla().WithDomain("urdt_ussdhandler")
|
||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
translationDir = path.Join(scriptDir, "locale")
|
translationDir = path.Join(scriptDir, "locale")
|
||||||
)
|
)
|
||||||
@ -42,67 +47,29 @@ const (
|
|||||||
AccountCreated = "ACCOUNTCREATED"
|
AccountCreated = "ACCOUNTCREATED"
|
||||||
)
|
)
|
||||||
|
|
||||||
func toBytes(s string) []byte {
|
|
||||||
return []byte(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
type FSData struct {
|
|
||||||
Path string
|
|
||||||
St *state.State
|
|
||||||
}
|
|
||||||
|
|
||||||
// FlagManager handles centralized flag management
|
|
||||||
type FlagManager struct {
|
|
||||||
parser *asm.FlagParser
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewFlagManager creates a new FlagManager instance
|
|
||||||
func NewFlagManager(csvPath string) (*FlagManager, error) {
|
|
||||||
parser := asm.NewFlagParser()
|
|
||||||
_, err := parser.Load(csvPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to load flag parser: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &FlagManager{
|
|
||||||
parser: parser,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFlag retrieves a flag value by its label
|
|
||||||
func (fm *FlagManager) GetFlag(label string) (uint32, error) {
|
|
||||||
return fm.parser.GetFlag(label)
|
|
||||||
}
|
|
||||||
|
|
||||||
type Handlers struct {
|
type Handlers struct {
|
||||||
fs *FSData
|
st *state.State
|
||||||
db *gdbm.Database
|
ca cache.Memory
|
||||||
flagManager *FlagManager
|
userdataStore db.Db
|
||||||
accountFileHandler utils.AccountFileHandlerInterface
|
flagManager *asm.FlagParser
|
||||||
|
accountFileHandler *utils.AccountFileHandler
|
||||||
accountService server.AccountServiceInterface
|
accountService server.AccountServiceInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandlers(dir string, st *state.State, sessionId string) (*Handlers, error) {
|
func NewHandlers(appFlags *asm.FlagParser, pe *persist.Persister, userdataStore db.Db) (*Handlers, error) {
|
||||||
filename := path.Join(scriptDir, sessionId+"_userdata.gdbm")
|
h := &Handlers{
|
||||||
db, err := gdbm.Open(filename, gdbm.ModeWrcreat)
|
st: pe.GetState(),
|
||||||
if err != nil {
|
ca: pe.GetMemory(),
|
||||||
panic(err)
|
userdataStore: userdataStore,
|
||||||
}
|
flagManager: appFlags,
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
accountFileHandler: utils.NewAccountFileHandler(userdataStore),
|
||||||
flagManager, err := NewFlagManager(pfp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to create flag manager: %v", err)
|
|
||||||
}
|
|
||||||
return &Handlers{
|
|
||||||
db: db,
|
|
||||||
fs: &FSData{
|
|
||||||
Path: dir,
|
|
||||||
St: st,
|
|
||||||
},
|
|
||||||
flagManager: flagManager,
|
|
||||||
accountFileHandler: utils.NewAccountFileHandler(dir + "_data"),
|
|
||||||
accountService: &server.AccountService{},
|
accountService: &server.AccountService{},
|
||||||
}, nil
|
}
|
||||||
|
if h.st == nil || h.ca == nil || h.userdataStore == nil || h.flagManager == nil {
|
||||||
|
logg.Errorf("have nil for essential value in handler", "state", h.st, "cache", h.ca, "store", h.userdataStore, "flags", h.flagManager)
|
||||||
|
return nil, fmt.Errorf("have nil for essential value")
|
||||||
|
}
|
||||||
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define the regex pattern as a constant
|
// Define the regex pattern as a constant
|
||||||
@ -138,45 +105,58 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateAccount checks if any account exists on the JSON data file, and if not
|
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||||
// creates an account on the API,
|
|
||||||
// sets the default values and flags
|
|
||||||
func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
|
||||||
res := resource.Result{}
|
|
||||||
|
|
||||||
_, err := h.db.Fetch([]byte(AccountCreated))
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, gdbm.ErrItemNotFound) {
|
|
||||||
accountResp, err := h.accountService.CreateAccount()
|
accountResp, err := h.accountService.CreateAccount()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed")
|
flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed")
|
||||||
res.FlagSet = append(res.FlagSet, flag_account_creation_failed)
|
res.FlagSet = append(res.FlagSet, flag_account_creation_failed)
|
||||||
return res, err
|
return err
|
||||||
}
|
}
|
||||||
data := map[string]string{
|
// data := map[string]string{
|
||||||
TrackingIdKey: accountResp.Result.TrackingId,
|
// TrackingIdKey: accountResp.Result.TrackingId,
|
||||||
PublicKeyKey: accountResp.Result.PublicKey,
|
// PublicKeyKey: accountResp.Result.PublicKey,
|
||||||
CustodialIdKey: accountResp.Result.CustodialId.String(),
|
// CustodialIdKey: accountResp.Result.CustodialId.String(),
|
||||||
|
// }
|
||||||
|
data := map[utils.DataTyp]string{
|
||||||
|
utils.DATA_TRACKING_ID: accountResp.Result.TrackingId,
|
||||||
|
utils.DATA_PUBLIC_KEY: accountResp.Result.PublicKey,
|
||||||
|
utils.DATA_CUSTODIAL_ID: accountResp.Result.CustodialId.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range data {
|
for key, value := range data {
|
||||||
err := h.db.Store(toBytes(key), toBytes(value), true)
|
err := utils.WriteEntry(ctx, h.userdataStore, sessionId, key, []byte(value))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
key := []byte(AccountCreated)
|
// NOTE: this is covered by the flag, no?
|
||||||
value := []byte("1")
|
//key := []byte(AccountCreated)
|
||||||
h.db.Store(key, value, true)
|
//value := []byte("1")
|
||||||
|
//h.db.Store(key, value, true)
|
||||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||||
return res, err
|
return err
|
||||||
} else {
|
|
||||||
return res, err
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return res, nil
|
// CreateAccount checks if any account exists on the JSON data file, and if not
|
||||||
|
// creates an account on the API,
|
||||||
|
// sets the default values and flags
|
||||||
|
func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
|
var err error
|
||||||
|
var res resource.Result
|
||||||
|
|
||||||
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
|
if !ok {
|
||||||
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_CREATED)
|
||||||
|
if err != nil {
|
||||||
|
if db.IsNotFound(err) {
|
||||||
|
err = h.createAccountNoExist(ctx, sessionId, &res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SavePin persists the user's PIN choice into the filesystem
|
// SavePin persists the user's PIN choice into the filesystem
|
||||||
@ -444,12 +424,12 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.db.Store(toBytes(AccountStatus), toBytes(status), true)
|
err = h.db.Store([]byte(AccountStatus), []byte(status), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.db.Store(toBytes(TrackingIdKey), toBytes(status), true)
|
err = h.db.Store([]byte(TrackingIdKey), []byte(status), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,25 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AccountFileHandler struct {
|
type AccountFileHandler struct {
|
||||||
FilePath string
|
store db.Db
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAccountFileHandler(path string) *AccountFileHandler {
|
func NewAccountFileHandler(store db.Db) *AccountFileHandler {
|
||||||
return &AccountFileHandler{FilePath: path}
|
return &AccountFileHandler{
|
||||||
}
|
store: store,
|
||||||
|
}
|
||||||
func (afh *AccountFileHandler) ReadAccountData() (map[string]string, error) {
|
|
||||||
jsonData, err := os.ReadFile(afh.FilePath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (afh *AccountFileHandler) ReadAccountData(ctx context.Context, sessionId string) (map[string]string, error) {
|
||||||
var accountData map[string]string
|
var accountData map[string]string
|
||||||
|
jsonData, err := ReadEntry(ctx, afh.store, sessionId, DATA_ACCOUNT)
|
||||||
err = json.Unmarshal(jsonData, &accountData)
|
err = json.Unmarshal(jsonData, &accountData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -28,19 +28,10 @@ func (afh *AccountFileHandler) ReadAccountData() (map[string]string, error) {
|
|||||||
return accountData, nil
|
return accountData, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (afh *AccountFileHandler) WriteAccountData(accountData map[string]string) error {
|
func (afh *AccountFileHandler) WriteAccountData(ctx context.Context, sessionId string, accountData map[string]string) error {
|
||||||
jsonData, err := json.Marshal(accountData)
|
b, err := json.Marshal(accountData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return WriteEntry(ctx, afh.store, sessionId, DATA_ACCOUNT, b)
|
||||||
return os.WriteFile(afh.FilePath, jsonData, 0644)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (afh *AccountFileHandler) EnsureFileExists() error {
|
|
||||||
f, err := os.OpenFile(afh.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return f.Close()
|
|
||||||
}
|
}
|
||||||
|
48
internal/utils/db.go
Normal file
48
internal/utils/db.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DataTyp uint16
|
||||||
|
|
||||||
|
const (
|
||||||
|
DATA_ACCOUNT DataTyp = iota
|
||||||
|
DATA_ACCOUNT_CREATED
|
||||||
|
DATA_TRACKING_ID
|
||||||
|
DATA_PUBLIC_KEY
|
||||||
|
DATA_CUSTODIAL_ID
|
||||||
|
)
|
||||||
|
|
||||||
|
func typToBytes(typ DataTyp) []byte {
|
||||||
|
var b []byte
|
||||||
|
binary.BigEndian.PutUint16(b, uint16(typ))
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func packKey(typ DataTyp, data []byte) []byte {
|
||||||
|
v := typToBytes(typ)
|
||||||
|
return append(v, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp) ([]byte, error) {
|
||||||
|
store.SetPrefix(db.DATATYPE_USERDATA)
|
||||||
|
store.SetSession(sessionId)
|
||||||
|
k := packKey(typ, []byte(sessionId))
|
||||||
|
b, err := store.Get(ctx, k)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b, nil
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp, value []byte) error {
|
||||||
|
store.SetPrefix(db.DATATYPE_USERDATA)
|
||||||
|
store.SetSession(sessionId)
|
||||||
|
k := packKey(typ, []byte(sessionId))
|
||||||
|
return store.Put(ctx, k, value)
|
||||||
|
}
|
@ -1,13 +1 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type AccountFileHandlerInterface interface {
|
|
||||||
EnsureFileExists() error
|
|
||||||
ReadAccountData() (map[string]string, error)
|
|
||||||
WriteAccountData(data map[string]string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user