Compare commits
No commits in common. "71973829111e6fe72e662f3be1886c2675cc1d39" and "929e8129929902eee8fe3083297e6e8dda1c91e7" have entirely different histories.
7197382911
...
929e812992
@ -12,13 +12,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/asm"
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
|
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/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||||
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -26,9 +30,9 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
type atRequestParser struct{}
|
type atRequestParser struct {}
|
||||||
|
|
||||||
func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
func(arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
||||||
rqv, ok := rq.(*http.Request)
|
rqv, ok := rq.(*http.Request)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", handlers.ErrInvalidRequest
|
return "", handlers.ErrInvalidRequest
|
||||||
@ -45,7 +49,7 @@ func (arp *atRequestParser) GetSessionId(rq any) (string, error) {
|
|||||||
return phoneNumber, nil
|
return phoneNumber, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (arp *atRequestParser) GetInput(rq any) ([]byte, error) {
|
func(arp *atRequestParser) GetInput(rq any) ([]byte, error) {
|
||||||
rqv, ok := rq.(*http.Request)
|
rqv, ok := rq.(*http.Request)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, handlers.ErrInvalidRequest
|
return nil, handlers.ErrInvalidRequest
|
||||||
@ -64,6 +68,96 @@ func (arp *atRequestParser) GetInput(rq any) ([]byte, error) {
|
|||||||
return []byte(parts[len(parts)-1]), nil
|
return []byte(parts[len(parts)-1]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
|
||||||
|
flagParser := asm.NewFlagParser().WithDebug()
|
||||||
|
_, err := flagParser.Load(fp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return flagParser, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||||
|
|
||||||
|
ussdHandlers, err := ussd.NewHandlers(appFlags, userdataStore)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rs.AddLocalFunc("set_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)
|
||||||
|
rs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin)
|
||||||
|
rs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
||||||
|
rs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
||||||
|
rs.AddLocalFunc("quit_with_help",ussdHandlers.QuitWithHelp)
|
||||||
|
|
||||||
|
return ussdHandlers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDbDir(dbDir string) error {
|
||||||
|
err := os.MkdirAll(dbDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStateStore(dbDir string, ctx context.Context) (db.Db, error) {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "state.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
return store, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "userdata.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
func getResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
||||||
|
store := fsdb.NewFsDb()
|
||||||
|
err := store.Connect(ctx, resourceDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rfs := resource.NewDbResource(store)
|
||||||
|
return rfs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
@ -85,6 +179,11 @@ func main() {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
flagParser, err := getFlags(pfp, true)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
@ -98,21 +197,19 @@ func main() {
|
|||||||
cfg.EngineDebug = true
|
cfg.EngineDebug = true
|
||||||
}
|
}
|
||||||
|
|
||||||
menuStorageService := storage.MenuStorageService{}
|
rs, err := getResource(resourceDir, ctx)
|
||||||
|
|
||||||
rs, err := menuStorageService.GetResource(scriptDir, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = menuStorageService.EnsureDbDir(dbDir)
|
err = ensureDbDir(dbDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
userdataStore := menuStorageService.GetUserdataDb(dbDir, ctx)
|
userdataStore := getUserdataDb(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -124,21 +221,13 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
hl, err := getHandler(flagParser, dbResource, userdataStore)
|
||||||
lhs.WithDataStore(&userdataStore)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
hl, err := lhs.GetHandler()
|
stateStore, err := getStateStore(dbDir, ctx)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
stateStore, err := menuStorageService.GetStateStore(dbDir, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -9,12 +9,16 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/asm"
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
|
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/logging"
|
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
"git.defalsify.org/vise.git/logging"
|
||||||
|
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -27,14 +31,103 @@ type asyncRequestParser struct {
|
|||||||
input []byte
|
input []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *asyncRequestParser) GetSessionId(r any) (string, error) {
|
func(p *asyncRequestParser) GetSessionId(r any) (string, error) {
|
||||||
return p.sessionId, nil
|
return p.sessionId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *asyncRequestParser) GetInput(r any) ([]byte, error) {
|
func(p *asyncRequestParser) GetInput(r any) ([]byte, error) {
|
||||||
return p.input, nil
|
return p.input, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
|
||||||
|
flagParser := asm.NewFlagParser().WithDebug()
|
||||||
|
_, err := flagParser.Load(fp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return flagParser, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||||
|
|
||||||
|
ussdHandlers, err := ussd.NewHandlers(appFlags, userdataStore)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rs.AddLocalFunc("set_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)
|
||||||
|
rs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin)
|
||||||
|
rs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
||||||
|
rs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
||||||
|
rs.AddLocalFunc("quit_with_help",ussdHandlers.QuitWithHelp)
|
||||||
|
|
||||||
|
return ussdHandlers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDbDir(dbDir string) error {
|
||||||
|
err := os.MkdirAll(dbDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStateStore(dbDir string, ctx context.Context) (db.Db, error) {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "state.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
return store, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "userdata.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
func getResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
||||||
|
store := fsdb.NewFsDb()
|
||||||
|
err := store.Connect(ctx, resourceDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rfs := resource.NewDbResource(store)
|
||||||
|
return rfs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var sessionId string
|
var sessionId string
|
||||||
var dbDir string
|
var dbDir string
|
||||||
@ -58,6 +151,11 @@ func main() {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
flagParser, err := getFlags(pfp, true)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
@ -71,20 +169,19 @@ func main() {
|
|||||||
cfg.EngineDebug = true
|
cfg.EngineDebug = true
|
||||||
}
|
}
|
||||||
|
|
||||||
menuStorageService := storage.MenuStorageService{}
|
rs, err := getResource(resourceDir, ctx)
|
||||||
rs, err := menuStorageService.GetResource(scriptDir, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = menuStorageService.EnsureDbDir(dbDir)
|
err = ensureDbDir(dbDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
userdataStore := menuStorageService.GetUserdataDb(dbDir, ctx)
|
userdataStore := getUserdataDb(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -96,16 +193,13 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
hl, err := getHandler(flagParser, dbResource, userdataStore)
|
||||||
lhs.WithDataStore(&userdataStore)
|
|
||||||
|
|
||||||
hl, err := lhs.GetHandler()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
stateStore, err := menuStorageService.GetStateStore(dbDir, ctx)
|
stateStore, err := getStateStore(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
121
cmd/http/main.go
121
cmd/http/main.go
@ -11,13 +11,17 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/asm"
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
|
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/logging"
|
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
|
"git.defalsify.org/vise.git/logging"
|
||||||
|
|
||||||
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||||
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
httpserver "git.grassecon.net/urdt/ussd/internal/http"
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -25,6 +29,95 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
|
||||||
|
flagParser := asm.NewFlagParser().WithDebug()
|
||||||
|
_, err := flagParser.Load(fp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return flagParser, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||||
|
|
||||||
|
ussdHandlers, err := ussd.NewHandlers(appFlags, userdataStore)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rs.AddLocalFunc("set_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)
|
||||||
|
rs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin)
|
||||||
|
rs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
||||||
|
rs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
||||||
|
rs.AddLocalFunc("quit_with_help",ussdHandlers.QuitWithHelp)
|
||||||
|
|
||||||
|
return ussdHandlers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDbDir(dbDir string) error {
|
||||||
|
err := os.MkdirAll(dbDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStateStore(dbDir string, ctx context.Context) (db.Db, error) {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "state.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
return store, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "userdata.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
func getResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
||||||
|
store := fsdb.NewFsDb()
|
||||||
|
err := store.Connect(ctx, resourceDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rfs := resource.NewDbResource(store)
|
||||||
|
return rfs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var resourceDir string
|
var resourceDir string
|
||||||
@ -46,6 +139,11 @@ func main() {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
flagParser, err := getFlags(pfp, true)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
@ -59,20 +157,19 @@ func main() {
|
|||||||
cfg.EngineDebug = true
|
cfg.EngineDebug = true
|
||||||
}
|
}
|
||||||
|
|
||||||
menuStorageService := storage.MenuStorageService{}
|
rs, err := getResource(resourceDir, ctx)
|
||||||
rs, err := menuStorageService.GetResource(scriptDir, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = menuStorageService.EnsureDbDir(dbDir)
|
err = ensureDbDir(dbDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
userdataStore := menuStorageService.GetUserdataDb(dbDir, ctx)
|
userdataStore := getUserdataDb(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -84,21 +181,13 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
hl, err := getHandler(flagParser, dbResource, userdataStore)
|
||||||
lhs.WithDataStore(&userdataStore)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
hl, err := lhs.GetHandler()
|
stateStore, err := getStateStore(dbDir, ctx)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
stateStore, err := menuStorageService.GetStateStore(dbDir, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
140
cmd/main.go
140
cmd/main.go
@ -7,11 +7,15 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/asm"
|
||||||
|
"git.defalsify.org/vise.git/db"
|
||||||
|
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/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
|
"git.defalsify.org/vise.git/persist"
|
||||||
"git.defalsify.org/vise.git/resource"
|
"git.defalsify.org/vise.git/resource"
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -19,6 +23,106 @@ var (
|
|||||||
scriptDir = path.Join("services", "registration")
|
scriptDir = path.Join("services", "registration")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func getParser(fp string, debug bool) (*asm.FlagParser, error) {
|
||||||
|
flagParser := asm.NewFlagParser().WithDebug()
|
||||||
|
_, err := flagParser.Load(fp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return flagParser, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||||
|
|
||||||
|
ussdHandlers, err := ussd.NewHandlers(appFlags, userdataStore)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ussdHandlers = ussdHandlers.WithPersister(pe)
|
||||||
|
rs.AddLocalFunc("set_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)
|
||||||
|
rs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin)
|
||||||
|
rs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
||||||
|
rs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
||||||
|
rs.AddLocalFunc("quit_with_help",ussdHandlers.QuitWithHelp)
|
||||||
|
|
||||||
|
return ussdHandlers, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ensureDbDir(dbDir string) error {
|
||||||
|
err := os.MkdirAll(dbDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPersister(dbDir string, ctx context.Context) (*persist.Persister, error) {
|
||||||
|
err := ensureDbDir(dbDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "state.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
pr := persist.NewPersister(store)
|
||||||
|
return pr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
||||||
|
store := gdbmdb.NewGdbmDb()
|
||||||
|
storeFile := path.Join(dbDir, "userdata.gdbm")
|
||||||
|
store.Connect(ctx, storeFile)
|
||||||
|
|
||||||
|
return store
|
||||||
|
}
|
||||||
|
|
||||||
|
func getResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
||||||
|
store := fsdb.NewFsDb()
|
||||||
|
err := store.Connect(ctx, resourceDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
rfs := resource.NewDbResource(store)
|
||||||
|
return rfs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) *engine.DefaultEngine {
|
||||||
|
en := engine.NewEngine(cfg, rs)
|
||||||
|
en = en.WithPersister(pr)
|
||||||
|
return en
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var dbDir string
|
var dbDir string
|
||||||
var size uint
|
var size uint
|
||||||
@ -35,6 +139,11 @@ func main() {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
pfp := path.Join(scriptDir, "pp.csv")
|
pfp := path.Join(scriptDir, "pp.csv")
|
||||||
|
flagParser, err := getParser(pfp, true)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
cfg := engine.Config{
|
cfg := engine.Config{
|
||||||
Root: "root",
|
Root: "root",
|
||||||
@ -43,27 +152,19 @@ func main() {
|
|||||||
FlagCount: uint32(16),
|
FlagCount: uint32(16),
|
||||||
}
|
}
|
||||||
|
|
||||||
menuStorageService := storage.MenuStorageService{}
|
rs, err := getResource(scriptDir, ctx)
|
||||||
|
|
||||||
err := menuStorageService.EnsureDbDir(dbDir)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
rs, err := menuStorageService.GetResource(scriptDir, ctx)
|
pe, err := getPersister(dbDir, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
pe, err := menuStorageService.GetPersister(dbDir, ctx)
|
store := getUserdataDb(dbDir, ctx)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
userdatastore := menuStorageService.GetUserdataDb(dbDir, ctx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -75,22 +176,13 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
hl, err := getHandler(flagParser, dbResource, pe, store)
|
||||||
lhs.WithDataStore(&userdatastore)
|
|
||||||
lhs.WithPersister(pe)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
fmt.Fprintf(os.Stderr, err.Error())
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
hl, err := lhs.GetHandler()
|
en := getEngine(cfg, rs, pe)
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
en := lhs.GetEngine()
|
|
||||||
en = en.WithFirst(hl.Init)
|
en = en.WithFirst(hl.Init)
|
||||||
if debug {
|
if debug {
|
||||||
en = en.WithDebug(nil)
|
en = en.WithDebug(nil)
|
||||||
|
@ -1,105 +0,0 @@
|
|||||||
package handlers
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.defalsify.org/vise.git/asm"
|
|
||||||
"git.defalsify.org/vise.git/db"
|
|
||||||
"git.defalsify.org/vise.git/engine"
|
|
||||||
"git.defalsify.org/vise.git/persist"
|
|
||||||
"git.defalsify.org/vise.git/resource"
|
|
||||||
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
|
||||||
)
|
|
||||||
|
|
||||||
type HandlerService interface {
|
|
||||||
GetHandler() (*ussd.Handlers, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getParser(fp string, debug bool) (*asm.FlagParser, error) {
|
|
||||||
flagParser := asm.NewFlagParser().WithDebug()
|
|
||||||
_, err := flagParser.Load(fp)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return flagParser, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type LocalHandlerService struct {
|
|
||||||
Parser *asm.FlagParser
|
|
||||||
DbRs *resource.DbResource
|
|
||||||
Pe *persist.Persister
|
|
||||||
UserdataStore *db.Db
|
|
||||||
Cfg engine.Config
|
|
||||||
Rs resource.Resource
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLocalHandlerService(fp string, debug bool, dbResource *resource.DbResource, cfg engine.Config, rs resource.Resource) (*LocalHandlerService, error) {
|
|
||||||
parser, err := getParser(fp, debug)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &LocalHandlerService{
|
|
||||||
Parser: parser,
|
|
||||||
DbRs: dbResource,
|
|
||||||
Cfg: cfg,
|
|
||||||
Rs: rs,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (localHandlerService *LocalHandlerService) WithPersister(Pe *persist.Persister) {
|
|
||||||
localHandlerService.Pe = Pe
|
|
||||||
}
|
|
||||||
|
|
||||||
func (localHandlerService *LocalHandlerService) WithDataStore(db *db.Db) {
|
|
||||||
localHandlerService.UserdataStore = db
|
|
||||||
}
|
|
||||||
|
|
||||||
func (localHandlerService *LocalHandlerService) GetHandler() (*ussd.Handlers, error) {
|
|
||||||
ussdHandlers, err := ussd.NewHandlers(localHandlerService.Parser, *localHandlerService.UserdataStore)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ussdHandlers = ussdHandlers.WithPersister(localHandlerService.Pe)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("set_language", ussdHandlers.SetLanguage)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("create_account", ussdHandlers.CreateAccount)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_pin", ussdHandlers.SavePin)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("verify_pin", ussdHandlers.VerifyPin)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("check_identifier", ussdHandlers.CheckIdentifier)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("check_account_status", ussdHandlers.CheckAccountStatus)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("authorize_account", ussdHandlers.Authorize)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("quit", ussdHandlers.Quit)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("check_balance", ussdHandlers.CheckBalance)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("validate_recipient", ussdHandlers.ValidateRecipient)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("transaction_reset", ussdHandlers.TransactionReset)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("max_amount", ussdHandlers.MaxAmount)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("validate_amount", ussdHandlers.ValidateAmount)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("reset_transaction_amount", ussdHandlers.ResetTransactionAmount)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("get_recipient", ussdHandlers.GetRecipient)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("get_sender", ussdHandlers.GetSender)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("get_amount", ussdHandlers.GetAmount)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("reset_incorrect", ussdHandlers.ResetIncorrectPin)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_firstname", ussdHandlers.SaveFirstname)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_familyname", ussdHandlers.SaveFamilyname)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_gender", ussdHandlers.SaveGender)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_location", ussdHandlers.SaveLocation)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_yob", ussdHandlers.SaveYob)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_offerings", ussdHandlers.SaveOfferings)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("quit_with_balance", ussdHandlers.QuitWithBalance)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("reset_account_authorized", ussdHandlers.ResetAccountAuthorized)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("reset_allow_update", ussdHandlers.ResetAllowUpdate)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("get_profile_info", ussdHandlers.GetProfileInfo)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("verify_yob", ussdHandlers.VerifyYob)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("reset_incorrect_date_format", ussdHandlers.ResetIncorrectYob)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("set_reset_single_edit", ussdHandlers.SetResetSingleEdit)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("initiate_transaction", ussdHandlers.InitiateTransaction)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("save_temporary_pin", ussdHandlers.SaveTemporaryPin)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("verify_new_pin", ussdHandlers.VerifyNewPin)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("confirm_pin_change", ussdHandlers.ConfirmPinChange)
|
|
||||||
localHandlerService.DbRs.AddLocalFunc("quit_with_help", ussdHandlers.QuitWithHelp)
|
|
||||||
|
|
||||||
return ussdHandlers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (localHandlerService *LocalHandlerService) GetEngine() *engine.DefaultEngine {
|
|
||||||
en := engine.NewEngine(localHandlerService.Cfg, localHandlerService.Rs)
|
|
||||||
en = en.WithPersister(localHandlerService.Pe)
|
|
||||||
return en
|
|
||||||
}
|
|
@ -1,64 +0,0 @@
|
|||||||
package storage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/db"
|
|
||||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
|
||||||
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
|
|
||||||
"git.defalsify.org/vise.git/persist"
|
|
||||||
"git.defalsify.org/vise.git/resource"
|
|
||||||
)
|
|
||||||
|
|
||||||
type StorageService interface {
|
|
||||||
GetPersister(dbDir string, ctx context.Context) (*persist.Persister, error)
|
|
||||||
GetUserdataDb(dbDir string, ctx context.Context) db.Db
|
|
||||||
GetResource(resourceDir string, ctx context.Context) (resource.Resource, error)
|
|
||||||
EnsureDbDir(dbDir string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
type MenuStorageService struct{}
|
|
||||||
|
|
||||||
func (menuStorageService *MenuStorageService) GetPersister(dbDir string, ctx context.Context) (*persist.Persister, error) {
|
|
||||||
store := gdbmdb.NewGdbmDb()
|
|
||||||
storeFile := path.Join(dbDir, "state.gdbm")
|
|
||||||
store.Connect(ctx, storeFile)
|
|
||||||
pr := persist.NewPersister(store)
|
|
||||||
return pr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (menuStorageService *MenuStorageService) GetUserdataDb(dbDir string, ctx context.Context) db.Db {
|
|
||||||
store := gdbmdb.NewGdbmDb()
|
|
||||||
storeFile := path.Join(dbDir, "userdata.gdbm")
|
|
||||||
store.Connect(ctx, storeFile)
|
|
||||||
|
|
||||||
return store
|
|
||||||
}
|
|
||||||
|
|
||||||
func (menuStorageService *MenuStorageService) GetResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
|
||||||
store := fsdb.NewFsDb()
|
|
||||||
err := store.Connect(ctx, resourceDir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rfs := resource.NewDbResource(store)
|
|
||||||
return rfs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (menuStorageService *MenuStorageService) GetStateStore(dbDir string, ctx context.Context) (db.Db, error) {
|
|
||||||
store := gdbmdb.NewGdbmDb()
|
|
||||||
storeFile := path.Join(dbDir, "state.gdbm")
|
|
||||||
store.Connect(ctx, storeFile)
|
|
||||||
return store, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (menuStorageService *MenuStorageService) EnsureDbDir(dbDir string) error {
|
|
||||||
err := os.MkdirAll(dbDir, 0700)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("state dir create exited with error: %v\n", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user