From 5d8de80a181971a61bb8de3cef894fafc2700b86 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Wed, 11 Dec 2024 18:58:50 +0300 Subject: [PATCH 1/5] Write the error in the response --- internal/http/server.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/internal/http/server.go b/internal/http/server.go index 3ea0159..a6239c4 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -17,8 +17,7 @@ var ( type DefaultRequestParser struct { } - -func(rp *DefaultRequestParser) GetSessionId(rq any) (string, error) { +func (rp *DefaultRequestParser) GetSessionId(rq any) (string, error) { rqv, ok := rq.(*http.Request) if !ok { return "", handlers.ErrInvalidRequest @@ -30,7 +29,7 @@ func(rp *DefaultRequestParser) GetSessionId(rq any) (string, error) { return v, nil } -func(rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) { +func (rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) { rqv, ok := rq.(*http.Request) if !ok { return nil, handlers.ErrInvalidRequest @@ -53,25 +52,24 @@ func ToSessionHandler(h handlers.RequestHandler) *SessionHandler { } } -func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) { +func (f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) { s := err.Error() w.Header().Set("Content-Length", strconv.Itoa(len(s))) w.WriteHeader(code) - _, err = w.Write([]byte{}) + _, err = w.Write([]byte(s)) if err != nil { logg.Errorf("error writing error!!", "err", err, "olderr", s) w.WriteHeader(500) } - return } -func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func (f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { var code int var err error var perr error rqs := handlers.RequestSession{ - Ctx: req.Context(), + Ctx: req.Context(), Writer: w, } -- 2.45.2 From f8ea2daa736887e97733e24470ca9b4c7bdfe796 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 12 Dec 2024 19:55:01 +0300 Subject: [PATCH 2/5] initialize the restart state devtool --- devtools/restart_state/main.go | 110 +++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 devtools/restart_state/main.go diff --git a/devtools/restart_state/main.go b/devtools/restart_state/main.go new file mode 100644 index 0000000..136b38a --- /dev/null +++ b/devtools/restart_state/main.go @@ -0,0 +1,110 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + "path" + + "git.defalsify.org/vise.git/engine" + "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/resource" + + // "git.defalsify.org/vise.git/persist" + "git.grassecon.net/urdt/ussd/config" + "git.grassecon.net/urdt/ussd/initializers" + "git.grassecon.net/urdt/ussd/remote" + + "git.grassecon.net/urdt/ussd/internal/handlers" + "git.grassecon.net/urdt/ussd/internal/storage" +) + +var ( + logg = logging.NewVanilla() + scriptDir = path.Join("services", "registration") +) + +func init() { + initializers.LoadEnvVariables() +} + +func main() { + config.LoadConfig() + + var dbDir string + var sessionId string + var database string + var engineDebug bool + var size uint + + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") + flag.StringVar(&database, "db", "gdbm", "database to be used") + flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") + flag.BoolVar(&engineDebug, "d", false, "use engine debug output") + flag.Parse() + + ctx := context.Background() + ctx = context.WithValue(ctx, "SessionId", sessionId) + ctx = context.WithValue(ctx, "Database", database) + + pfp := path.Join(scriptDir, "pp.csv") + + cfg := engine.Config{ + Root: "root", + SessionId: sessionId, + OutputSize: uint32(size), + FlagCount: uint32(128), + } + + resourceDir := scriptDir + menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) + + err := menuStorageService.EnsureDbDir() + 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) + } + + pe, err := menuStorageService.GetPersister(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) + } + + + // initialize the persister + + // get the state + + // restart the state + + // persist the state + + // exit + + st := pe.GetState() + + if st == nil { + logg.ErrorCtxf(ctx, "state fail in devtool", "state", st) + fmt.Errorf("cannot get state") + os.Exit(1) + } + + st.Restart() + + + os.Exit(1) +} -- 2.45.2 From 5428626c3f6cef24196be7d391b20b794ec284b1 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 17 Dec 2024 17:56:56 +0300 Subject: [PATCH 3/5] cleaned up the restart_state --- devtools/restart_state/main.go | 35 +--------------------------------- 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/devtools/restart_state/main.go b/devtools/restart_state/main.go index 136b38a..3068a38 100644 --- a/devtools/restart_state/main.go +++ b/devtools/restart_state/main.go @@ -7,16 +7,9 @@ import ( "os" "path" - "git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/logging" - "git.defalsify.org/vise.git/resource" - - // "git.defalsify.org/vise.git/persist" "git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/initializers" - "git.grassecon.net/urdt/ussd/remote" - - "git.grassecon.net/urdt/ussd/internal/handlers" "git.grassecon.net/urdt/ussd/internal/storage" ) @@ -35,28 +28,16 @@ func main() { var dbDir string var sessionId string var database string - var engineDebug bool - var size uint - + flag.StringVar(&sessionId, "session-id", "075xx2123", "session id") flag.StringVar(&database, "db", "gdbm", "database to be used") flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from") - flag.BoolVar(&engineDebug, "d", false, "use engine debug output") flag.Parse() ctx := context.Background() ctx = context.WithValue(ctx, "SessionId", sessionId) ctx = context.WithValue(ctx, "Database", database) - pfp := path.Join(scriptDir, "pp.csv") - - cfg := engine.Config{ - Root: "root", - SessionId: sessionId, - OutputSize: uint32(size), - FlagCount: uint32(128), - } - resourceDir := scriptDir menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir) @@ -66,25 +47,12 @@ func main() { os.Exit(1) } - rs, err := menuStorageService.GetResource(ctx) - if err != nil { - fmt.Fprintf(os.Stderr, err.Error()) - os.Exit(1) - } - pe, err := menuStorageService.GetPersister(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) - } - - // initialize the persister // get the state @@ -105,6 +73,5 @@ func main() { st.Restart() - os.Exit(1) } -- 2.45.2 From 553470618913fca61232761e3ffcfa8efe00cbab Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 17 Dec 2024 17:58:08 +0300 Subject: [PATCH 4/5] reset the state when input is nil --- internal/handlers/ussd/menuhandler.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index c5356d0..c2b41b2 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -124,6 +124,16 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource h.st = h.pe.GetState() h.ca = h.pe.GetMemory() + + fmt.Println("Pre restart state:", h.st) + + if len(input) == 0 { + h.st.Restart() + h.st = h.pe.GetState() + } + + fmt.Println("New state:", h.st) + sessionId, _ := ctx.Value("SessionId").(string) flag_admin_privilege, _ := h.flagManager.GetFlag("flag_admin_privilege") -- 2.45.2 From be215d3f75ed64a1794e87497a3682b5050842b7 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 20 Dec 2024 12:26:07 +0300 Subject: [PATCH 5/5] set the code to an empty byte for it to move to the top node --- internal/handlers/ussd/menuhandler.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index b3c6f1d..f509ff4 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -124,16 +124,11 @@ func (h *Handlers) Init(ctx context.Context, sym string, input []byte) (resource h.st = h.pe.GetState() h.ca = h.pe.GetMemory() - - fmt.Println("Pre restart state:", h.st) - if len(input) == 0 { - h.st.Restart() - h.st = h.pe.GetState() + // move to the top node + h.st.Code = []byte{} } - fmt.Println("New state:", h.st) - sessionId, _ := ctx.Value("SessionId").(string) flag_admin_privilege, _ := h.flagManager.GetFlag("flag_admin_privilege") -- 2.45.2