Draft example of dev-0.1.0 refactor #34

Closed
lash wants to merge 3 commits from lash/draft-dev-0.1.0 into origin/wip-go-vise-dev-0.1.0
6 changed files with 248 additions and 243 deletions
Showing only changes of commit 7fc235c84c - Show all commits

View File

@ -10,152 +10,150 @@ 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 newMenuResource(rs *resource.DbResource) resource.Resource { func getFlags(fp string, debug bool) error {
return &menuResource{ Flags = NewFlagParser().WithDebug()
rs, flags, err := Flags.Load(fp)
}
}
func main() {
var dir string
var root string
var size uint
var sessionId string
flag.StringVar(&dir, "d", ".", "resource dir to read from")
flag.UintVar(&size, "s", 0, "max size of output")
flag.StringVar(&root, "root", "root", "entry point symbol")
flag.StringVar(&sessionId, "session-id", "default", "session id")
flag.Parse()
fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
ctx := context.Background()
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) return err
os.Exit(1)
} }
defer file.Close() }
reader := csv.NewReader(file)
// Iterate through the CSV records and register the flags func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
for { ussdHandlers, err := ussd.NewHandlers(appFlags pr.GetState(), dataStore)
record, err := reader.Read() if err != nil {
if err != nil { return nil, err
if err == io.EOF { }
break rs.AddLocalFunc("select_language", ussdHandlers.SetLanguage)
} rs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
fmt.Fprintf(os.Stderr, "Error reading CSV file: %v\n", err) rs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
os.Exit(1) 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)
// Ensure the record starts with "flag" and has at least 3 columns return ussdHandlers, nil
if len(record) < 3 || record[0] != "flag" { }
continue
}
flagName := record[1] func getDataPersister(dbDir string) (*persist.Persister, error) {
flagValue, err := strconv.Atoi(record[2]) err = os.MkdirAll(dp, 0700)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Failed to convert flag value %s to integer: %v\n", record[2], err) return nil, fmt.Errorf("state dir create exited with error: %v\n", err)
continue
}
// Register the flag
state.FlagDebugger.Register(uint32(flagValue), flagName)
} }
ca := cache.NewCache() dataStore := gdbmdb.NewGdbmDb()
dataStoreFile := path.Join(dbDir, "states.gdbm")
dataStore.Connect(ctx, dataStoreFile)
pr := persist.NewPersister(dataStore)
return pr
}
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) {
cfg := engine.Config{ cfg := engine.Config{
Root: "root", Root: sym,
SessionId: sessionId, SessionId: sessionId,
FlagCount: uint32(16), FlagCount: uint32(16),
} }
en := engine.NewEngine(cfg, rfs)
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) en = en.WithPersister(pr)
return en
}
fp := path.Join(dp, sessionId) func main() {
var dbDir string
ussdHandlers, err := ussd.NewHandlers(fp, pr.State, sessionId) var resourceDir string
var root string
var size uint
var sessionId string
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
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(&sessionId, "session-id", "default", "session id")
flag.Parse()
logg.Infof("starting session", "symbol", root, "dbdir", dbDir, "sessionid", sessionId, "outsize", size)
fl, err := getFlags()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "handler setup failed with error: %v\n", err) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
} }
rfs.AddLocalFunc("select_language", ussdHandlers.SetLanguage) rs, err := getResource(resourceDir)
rfs.AddLocalFunc("create_account", ussdHandlers.CreateAccount) if err != nil {
rfs.AddLocalFunc("save_pin", ussdHandlers.SavePin) fmt.Fprintf(os.Stderr, err.Error())
rfs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin) os.Exit(1)
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)
pr, err := getDataPersister(dbDir)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
store, err := getUserDb(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)
}
en := getEngine(cfg, rs, pr)
_, 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)

@ -1 +1 @@
Subproject commit 1f47a674d95380be8c387f410f0342eb72357df5 Subproject commit 326bdb5018ae81ee493371b56bd4564a8b35da59

View File

@ -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
} }
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
accountResp, err := h.accountService.CreateAccount()
if err != nil {
flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed")
res.FlagSet = append(res.FlagSet, flag_account_creation_failed)
return err
}
// data := map[string]string{
// TrackingIdKey: accountResp.Result.TrackingId,
// PublicKeyKey: accountResp.Result.PublicKey,
// 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 {
err := utils.WriteEntry(ctx, h.userdataStore, sessionId, key, []byte(value))
if err != nil {
return err
}
}
// NOTE: this is covered by the flag, no?
//key := []byte(AccountCreated)
//value := []byte("1")
//h.db.Store(key, value, true)
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
res.FlagSet = append(res.FlagSet, flag_account_created)
return err
}
// CreateAccount checks if any account exists on the JSON data file, and if not // CreateAccount checks if any account exists on the JSON data file, and if not
// creates an account on the API, // creates an account on the API,
// sets the default values and flags // sets the default values and flags
func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) { func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
res := resource.Result{} var err error
var res resource.Result
_, err := h.db.Fetch([]byte(AccountCreated)) sessionId, ok := ctx.Value("SessionId").(string)
if err != nil { if !ok {
if errors.Is(err, gdbm.ErrItemNotFound) { return res, fmt.Errorf("missing session")
accountResp, err := h.accountService.CreateAccount()
if err != nil {
flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed")
res.FlagSet = append(res.FlagSet, flag_account_creation_failed)
return res, err
}
data := map[string]string{
TrackingIdKey: accountResp.Result.TrackingId,
PublicKeyKey: accountResp.Result.PublicKey,
CustodialIdKey: accountResp.Result.CustodialId.String(),
}
for key, value := range data {
err := h.db.Store(toBytes(key), toBytes(value), true)
if err != nil {
return res, err
}
}
key := []byte(AccountCreated)
value := []byte("1")
h.db.Store(key, value, true)
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
res.FlagSet = append(res.FlagSet, flag_account_created)
return res, err
} else {
return res, err
}
} else {
return res, nil
} }
_, 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
} }

View File

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

View File

@ -1,13 +1 @@
package utils package utils
type AccountFileHandlerInterface interface {
EnsureFileExists() error
ReadAccountData() (map[string]string, error)
WriteAccountData(data map[string]string) error
}