From 681f293d3c01e59e66ea381ada3644b06507a772 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 10 Sep 2024 23:09:10 +0100 Subject: [PATCH] Externalize requestparser, flush persister on http request end --- cmd/http/main.go | 3 ++- go.mod | 2 +- go.sum | 4 ++-- internal/http/server.go | 16 +++++++++++----- internal/http/storage.go | 1 + 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/http/main.go b/cmd/http/main.go index 1edc1fe..071cf00 100644 --- a/cmd/http/main.go +++ b/cmd/http/main.go @@ -195,7 +195,8 @@ func main() { } defer stateStore.Close() - sh := httpserver.NewSessionHandler(cfg, rs, stateStore, userdataStore, hl.Init) + rp := &httpserver.DefaultRequestParser{} + sh := httpserver.NewSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl.Init) s := &http.Server{ Addr: fmt.Sprintf("%s:%s", host, strconv.Itoa(int(port))), Handler: sh, diff --git a/go.mod b/go.mod index dc20ac5..d11f113 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 v0.1.0-rc.2.0.20240907200911-15fe28c9d5b0 + git.defalsify.org/vise.git v0.1.0-rc.3.0.20240910220239-03876d1a78bd 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 d065871..0d09e94 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -git.defalsify.org/vise.git v0.1.0-rc.2.0.20240907200911-15fe28c9d5b0 h1:B9kE2XXjrYmHNIgRV6fR1WLWE8+z8OvDhJSc96lbGPQ= -git.defalsify.org/vise.git v0.1.0-rc.2.0.20240907200911-15fe28c9d5b0/go.mod h1:JDguWmcoWBdsnpw7PUjVZAEpdC/ubBmjdUBy3tjP63M= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240910220239-03876d1a78bd h1:pEYKEwz8qHiLfhQC+yYPv3jun7+WAa8xlGtaJpOErQ0= +git.defalsify.org/vise.git v0.1.0-rc.3.0.20240910220239-03876d1a78bd/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/http/server.go b/internal/http/server.go index 4ca7f73..eef41c9 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -16,10 +16,15 @@ var ( logg = logging.NewVanilla().WithDomain("httpserver") ) -type RequestParser struct { +type RequestParser interface { + GetSessionId(rq *http.Request) (string, error) + GetInput(rq *http.Request) ([]byte, error) } -func(rp *RequestParser) GetSessionId(rq *http.Request) (string, error) { +type DefaultRequestParser struct { +} + +func(rp *DefaultRequestParser) GetSessionId(rq *http.Request) (string, error) { v := rq.Header.Get("X-Vise-Session") if v == "" { return "", fmt.Errorf("no session found") @@ -27,7 +32,7 @@ func(rp *RequestParser) GetSessionId(rq *http.Request) (string, error) { return v, nil } -func(rp *RequestParser) GetInput(rq *http.Request) ([]byte, error) { +func(rp *DefaultRequestParser) GetInput(rq *http.Request) ([]byte, error) { defer rq.Body.Close() v, err := ioutil.ReadAll(rq.Body) if err != nil { @@ -44,12 +49,12 @@ type SessionHandler struct { provider StorageProvider } -func NewSessionHandler(cfg engine.Config, rs resource.Resource, stateDb db.Db, userdataDb db.Db, first resource.EntryFunc) *SessionHandler { +func NewSessionHandler(cfg engine.Config, rs resource.Resource, stateDb db.Db, userdataDb db.Db, rp RequestParser, first resource.EntryFunc) *SessionHandler { return &SessionHandler{ cfgTemplate: cfg, rs: rs, first: first, - rp: RequestParser{}, + rp: rp, provider: NewSimpleStorageProvider(stateDb, userdataDb), } } @@ -128,6 +133,7 @@ func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { f.writeError(w, 500, "Engine finish fail", err) return } + _ = r } diff --git a/internal/http/storage.go b/internal/http/storage.go index f8243cc..9b0cf44 100644 --- a/internal/http/storage.go +++ b/internal/http/storage.go @@ -22,6 +22,7 @@ type SimpleStorageProvider struct { func NewSimpleStorageProvider(stateStore db.Db, userdataStore db.Db) StorageProvider { pe := persist.NewPersister(stateStore) + pe = pe.WithFlush() return &SimpleStorageProvider{ Storage: Storage{ Persister: pe,