package main import ( "context" "flag" "fmt" "os" "os/signal" "path" "syscall" "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/lang" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" "git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/storage" "git.grassecon.net/urdt/ussd/remote" "git.grassecon.net/urdt/ussd/internal/args" ) var ( logg = logging.NewVanilla() scriptDir = path.Join("services", "registration") menuSeparator = ": " ) func init() { initializers.LoadEnvVariables() } type asyncRequestParser struct { sessionId string input []byte } func (p *asyncRequestParser) GetSessionId(ctx context.Context, r any) (string, error) { return p.sessionId, nil } func (p *asyncRequestParser) GetInput(r any) ([]byte, error) { return p.input, nil } func main() { config.LoadConfig() var connStr string var sessionId string var resourceDir string var size uint var database string var engineDebug bool var host string var port uint var err error var gettextDir string var langs args.LangVar flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir") flag.StringVar(&connStr, "c", "", "connection string") flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.UintVar(&size, "s", 160, "max size of output") flag.StringVar(&host, "h", initializers.GetEnv("HOST", "127.0.0.1"), "http host") flag.UintVar(&port, "p", initializers.GetEnvUint("PORT", 7123), "http port") flag.StringVar(&gettextDir, "gettext", "", "use gettext translations from given directory") flag.Var(&langs, "language", "add symbol resolution for language") flag.Parse() if connStr != "" { connStr = config.DbConn } connData, err := storage.ToConnData(config.DbConn) if err != nil { fmt.Fprintf(os.Stderr, "connstr err: %v", err) os.Exit(1) } logg.Infof("start command", "conn", connData, "resourcedir", resourceDir, "outputsize", size, "sessionId", sessionId) ctx := context.Background() ctx = context.WithValue(ctx, "Database", database) ln, err := lang.LanguageFromCode(config.DefaultLanguage) if err != nil { fmt.Fprintf(os.Stderr, "default language set error: %v", err) os.Exit(1) } ctx = context.WithValue(ctx, "Language", ln) pfp := path.Join(scriptDir, "pp.csv") cfg := engine.Config{ Root: "root", OutputSize: uint32(size), FlagCount: uint32(128), MenuSeparator: menuSeparator, } if engineDebug { cfg.EngineDebug = true } menuStorageService := storage.NewMenuStorageService(connData, resourceDir) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } rs, err := menuStorageService.GetResource(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } userdataStore, err := menuStorageService.GetUserdataDb(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } defer userdataStore.Close() dbResource, ok := rs.(*resource.DbResource) if !ok { os.Exit(1) } lhs, err := handlers.NewLocalHandlerService(ctx, pfp, true, dbResource, cfg, rs) lhs.SetDataStore(&userdataStore) accountService := remote.AccountService{} hl, err := lhs.GetHandler(&accountService) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } stateStore, err := menuStorageService.GetStateStore(ctx) if err != nil { fmt.Fprintf(os.Stderr, err.Error()) os.Exit(1) } defer stateStore.Close() rp := &asyncRequestParser{ sessionId: sessionId, } sh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl) cfg.SessionId = sessionId rqs := handlers.RequestSession{ Ctx: ctx, Writer: os.Stdout, Config: cfg, } cint := make(chan os.Signal) cterm := make(chan os.Signal) signal.Notify(cint, os.Interrupt, syscall.SIGINT) signal.Notify(cterm, os.Interrupt, syscall.SIGTERM) go func() { select { case _ = <-cint: case _ = <-cterm: } sh.Shutdown() }() for true { rqs, err = sh.Process(rqs) if err != nil { logg.ErrorCtxf(ctx, "error in process: %v", "err", err) fmt.Errorf("error in process: %v", err) os.Exit(1) } rqs, err = sh.Output(rqs) if err != nil { logg.ErrorCtxf(ctx, "error in output: %v", "err", err) fmt.Errorf("error in output: %v", err) os.Exit(1) } rqs, err = sh.Reset(rqs) if err != nil { logg.ErrorCtxf(ctx, "error in reset: %v", "err", err) fmt.Errorf("error in reset: %v", err) os.Exit(1) } fmt.Println("") _, err = fmt.Scanln(&rqs.Input) if err != nil { logg.ErrorCtxf(ctx, "error in input", "err", err) fmt.Errorf("error in input: %v", err) os.Exit(1) } } }