diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index be2ff01..c0db723 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -12,7 +12,6 @@ import ( "strings" "syscall" - "git.defalsify.org/vise.git/asm" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" @@ -65,15 +64,6 @@ func (arp *atRequestParser) GetInput(rq any) ([]byte, error) { 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 main() { var dbDir string var resourceDir string @@ -95,11 +85,6 @@ func main() { ctx := context.Background() pfp := path.Join(scriptDir, "pp.csv") - flagParser, err := getFlags(pfp, true) - - if err != nil { - os.Exit(1) - } cfg := engine.Config{ Root: "root", @@ -138,13 +123,9 @@ func main() { if !ok { os.Exit(1) } - lhs := handlers.LocalHandlerService{ - Parser: flagParser, - DbRs: dbResource, - UserdataStore: userdataStore, - Cfg: cfg, - Rs: rs, - } + + lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs) + lhs.WithDataStore(&userdataStore) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) diff --git a/cmd/async/main.go b/cmd/async/main.go index 5b8aa36..2ff0d1e 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -9,7 +9,6 @@ import ( "path" "syscall" - "git.defalsify.org/vise.git/asm" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" @@ -36,15 +35,6 @@ func (p *asyncRequestParser) GetInput(r any) ([]byte, error) { 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 main() { var sessionId string var dbDir string @@ -68,11 +58,6 @@ func main() { ctx := context.Background() pfp := path.Join(scriptDir, "pp.csv") - flagParser, err := getFlags(pfp, true) - - if err != nil { - os.Exit(1) - } cfg := engine.Config{ Root: "root", @@ -111,13 +96,8 @@ func main() { os.Exit(1) } - lhs := handlers.LocalHandlerService{ - Parser: flagParser, - DbRs: dbResource, - UserdataStore: userdataStore, - Cfg: cfg, - Rs: rs, - } + lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs) + lhs.WithDataStore(&userdataStore) hl, err := lhs.GetHandler() if err != nil { diff --git a/cmd/http/main.go b/cmd/http/main.go index 38fd441..7b74e19 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -11,7 +11,6 @@ import ( "strconv" "syscall" - "git.defalsify.org/vise.git/asm" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" @@ -26,23 +25,6 @@ var ( 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 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 main() { var dbDir string var resourceDir string @@ -64,11 +46,6 @@ func main() { ctx := context.Background() pfp := path.Join(scriptDir, "pp.csv") - flagParser, err := getFlags(pfp, true) - - if err != nil { - os.Exit(1) - } cfg := engine.Config{ Root: "root", @@ -107,12 +84,12 @@ func main() { os.Exit(1) } - lhs := handlers.LocalHandlerService{ - Parser: flagParser, - DbRs: dbResource, - UserdataStore: userdataStore, - Cfg: cfg, - Rs: rs, + lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs) + lhs.WithDataStore(&userdataStore) + + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + os.Exit(1) } hl, err := lhs.GetHandler() diff --git a/cmd/main.go b/cmd/main.go index ab36f30..7176bd3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -75,7 +75,9 @@ func main() { os.Exit(1) } - lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, pe, userdatastore, cfg, rs) + lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs) + lhs.WithDataStore(&userdatastore) + lhs.WithPersister(pe) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) diff --git a/internal/handlers/handlerservice.go b/internal/handlers/handlerservice.go index e6d4fb6..93c5a10 100644 --- a/internal/handlers/handlerservice.go +++ b/internal/handlers/handlerservice.go @@ -26,28 +26,34 @@ type LocalHandlerService struct { Parser *asm.FlagParser DbRs *resource.DbResource Pe *persist.Persister - UserdataStore db.Db + UserdataStore *db.Db Cfg engine.Config Rs resource.Resource } -func NewLocalHandlerService(fp string, debug bool, dbResource *resource.DbResource, Pe *persist.Persister, userDataStore db.Db, cfg engine.Config, rs resource.Resource) (*LocalHandlerService, error) { +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, - Pe: Pe, - UserdataStore: userDataStore, - Cfg: cfg, - Rs: rs, + 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) + ussdHandlers, err := ussd.NewHandlers(localHandlerService.Parser, *localHandlerService.UserdataStore) if err != nil { return nil, err }