From 9b4a4eeaf44a134118ff5d3658dd5a3cb24f3c3e Mon Sep 17 00:00:00 2001 From: lash Date: Thu, 12 Sep 2024 16:46:11 +0100 Subject: [PATCH] Temporary solution for make sure storage object gets put back in all cases of execution --- cmd/http/main.go | 1 - go.mod | 2 +- go.sum | 4 ++-- internal/handlers/base.go | 17 ++++++++++++++++- internal/handlers/single.go | 2 +- internal/http/server.go | 8 ++++---- internal/storage/storage.go | 12 ++++++------ 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/cmd/http/main.go b/cmd/http/main.go index 2006a47..1e132b4 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -191,7 +191,6 @@ func main() { defer stateStore.Close() rp := &httpserver.DefaultRequestParser{} - //sh := httpserver.NewSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl.Init) bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl) sh := httpserver.ToSessionHandler(bsh) s := &http.Server{ diff --git a/go.mod b/go.mod index a05d09c..e317ed4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.grassecon.net/urdt/ussd go 1.22.6 require ( - git.defalsify.org/vise.git 0d23e0dbb57fe63b6626527fddc86649cfc20f8f + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f github.com/alecthomas/assert/v2 v2.2.2 gopkg.in/leonelquinteros/gotext.v1 v1.3.1 ) diff --git a/go.sum b/go.sum index b40a422..1c00969 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911162138-1f2af8672dc7 h1:embPZDx0Sgpq6jp9vcZ1GVI0eum3PsPCmAfxAa/1KLI= -git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911162138-1f2af8672dc7/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f h1:CuJvG3NyMoRtHUim4aZdrfjjJBg2AId7z0yp7Q97bRM= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240911231817-0d23e0dbb57f/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= github.com/alecthomas/assert/v2 v2.2.2 h1:Z/iVC0xZfWTaFNE6bA3z07T86hd45Xe2eLt6WVy2bbk= github.com/alecthomas/assert/v2 v2.2.2/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ= github.com/alecthomas/participle/v2 v2.0.0 h1:Fgrq+MbuSsJwIkw3fEj9h75vDP0Er5JzepJ0/HNHv0g= diff --git a/internal/handlers/base.go b/internal/handlers/base.go index fba62c9..9f5c849 100644 --- a/internal/handlers/base.go +++ b/internal/handlers/base.go @@ -50,7 +50,7 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) rqs.Storage, err = f.provider.Get(rqs.Config.SessionId) if err != nil { - logg.ErrorCtxf(rqs.Ctx, "", "storage error", "err", err) + logg.ErrorCtxf(rqs.Ctx, "", "storage get error", err) return rqs, ErrStorage } @@ -58,6 +58,11 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) eni := f.GetEngine(rqs.Config, f.rs, rqs.Storage.Persister) en, ok := eni.(*engine.DefaultEngine) if !ok { + perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) + rqs.Storage = nil + if perr != nil { + logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr) + } return rqs, ErrEngineType } en = en.WithFirst(f.hn.Init) @@ -68,6 +73,11 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) r, err = rqs.Engine.Init(rqs.Ctx) if err != nil { + perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) + rqs.Storage = nil + if perr != nil { + logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr) + } return rqs, err } @@ -75,6 +85,11 @@ func(f *BaseSessionHandler) Process(rqs RequestSession) (RequestSession, error) r, err = rqs.Engine.Exec(rqs.Ctx, rqs.Input) } if err != nil { + perr := f.provider.Put(rqs.Config.SessionId, rqs.Storage) + rqs.Storage = nil + if perr != nil { + logg.ErrorCtxf(rqs.Ctx, "", "storage put error", perr) + } return rqs, err } diff --git a/internal/handlers/single.go b/internal/handlers/single.go index 3910be8..f786d41 100644 --- a/internal/handlers/single.go +++ b/internal/handlers/single.go @@ -32,7 +32,7 @@ type RequestSession struct { Config engine.Config Engine engine.Engine Input []byte - Storage storage.Storage + Storage *storage.Storage Writer io.Writer } diff --git a/internal/http/server.go b/internal/http/server.go index af5413a..3ea0159 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -68,6 +68,7 @@ func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) { 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(), @@ -109,14 +110,13 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain") rqs, err = f.Output(rqs) + rqs, perr = f.Reset(rqs) if err != nil { f.writeError(w, 500, err) return } - - rqs, err = f.Reset(rqs) - if err != nil { - f.writeError(w, 500, err) + if perr != nil { + f.writeError(w, 500, perr) return } } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index d009fd0..53f4392 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -11,31 +11,31 @@ type Storage struct { } type StorageProvider interface { - Get(sessionId string) (Storage, error) - Put(sessionId string, storage Storage) error + Get(sessionId string) (*Storage, error) + Put(sessionId string, storage *Storage) error Close() error } type SimpleStorageProvider struct { - Storage + *Storage } func NewSimpleStorageProvider(stateStore db.Db, userdataStore db.Db) StorageProvider { pe := persist.NewPersister(stateStore) pe = pe.WithFlush() return &SimpleStorageProvider{ - Storage: Storage{ + Storage: &Storage{ Persister: pe, UserdataDb: userdataStore, }, } } -func (p *SimpleStorageProvider) Get(sessionId string) (Storage, error) { +func (p *SimpleStorageProvider) Get(sessionId string) (*Storage, error) { return p.Storage, nil } -func (p *SimpleStorageProvider) Put(sessionId string, storage Storage) error { +func (p *SimpleStorageProvider) Put(sessionId string, storage *Storage) error { return nil }