From 977d14c52937147dfc175db3e9babb870722dfdd Mon Sep 17 00:00:00 2001 From: lash Date: Sat, 11 Jan 2025 08:33:52 +0000 Subject: [PATCH] Properly isolate http session handler --- request/request.go | 24 ------------ http/server.go => session/http/handler.go | 48 +++++++++++++---------- {http => session/http}/parse.go | 0 {http => session/http}/server_test.go | 0 4 files changed, 28 insertions(+), 44 deletions(-) rename http/server.go => session/http/handler.go (53%) rename {http => session/http}/parse.go (100%) rename {http => session/http}/server_test.go (100%) diff --git a/request/request.go b/request/request.go index 7d7654a..ce52ffb 100644 --- a/request/request.go +++ b/request/request.go @@ -3,8 +3,6 @@ package request import ( "context" "io" - "net/http" - "strconv" "git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/persist" @@ -27,7 +25,6 @@ type RequestSession struct { Continue bool } - // TODO: seems like can remove this. type RequestParser interface { GetSessionId(ctx context.Context, rq any) (string, error) @@ -43,24 +40,3 @@ type RequestHandler interface { Reset(rs RequestSession) (RequestSession, error) Shutdown() } - -type SessionHandler struct { - RequestHandler -} - -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(s)) - if err != nil { - logg.Errorf("error writing error!!", "err", err, "olderr", s) - w.WriteHeader(500) - } -} - -func ToSessionHandler(h RequestHandler) *SessionHandler { - return &SessionHandler{ - RequestHandler: h, - } -} diff --git a/http/server.go b/session/http/handler.go similarity index 53% rename from http/server.go rename to session/http/handler.go index 540db6c..f1d4cf4 100644 --- a/http/server.go +++ b/session/http/handler.go @@ -5,19 +5,21 @@ import ( "strconv" "git.defalsify.org/vise.git/logging" - - "git.grassecon.net/grassrootseconomics/visedriver/internal/handlers" "git.grassecon.net/grassrootseconomics/visedriver/request" + "git.grassecon.net/grassrootseconomics/visedriver/errors" ) var ( - logg = logging.NewVanilla().WithDomain("httpserver") + logg = logging.NewVanilla().WithDomain("visedriver.http.session") ) -type SessionHandler request.SessionHandler -// TODO: duplicated -func (f *SessionHandler) WriteError(w http.ResponseWriter, code int, err error) { +// HTTPSessionHandler implements the session handler for HTTP +type HTTPSessionHandler struct { + request.RequestHandler +} + +func (f *HTTPSessionHandler) WriteError(w http.ResponseWriter, code int, err error) { s := err.Error() w.Header().Set("Content-Length", strconv.Itoa(len(s))) w.WriteHeader(code) @@ -28,7 +30,13 @@ func (f *SessionHandler) WriteError(w http.ResponseWriter, code int, err error) } } -func (f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { +func NewHTTPSessionHandler(h request.RequestHandler) *HTTPSessionHandler { + return &HTTPSessionHandler{ + RequestHandler: h, + } +} + +func (hh *HTTPSessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { var code int var err error var perr error @@ -38,48 +46,48 @@ func (f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { Writer: w, } - rp := f.GetRequestParser() - cfg := f.GetConfig() + rp := hh.GetRequestParser() + cfg := hh.GetConfig() cfg.SessionId, err = rp.GetSessionId(req.Context(), req) if err != nil { logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err) - f.WriteError(w, 400, err) + hh.WriteError(w, 400, err) } rqs.Config = cfg rqs.Input, err = rp.GetInput(req) if err != nil { logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err) - f.WriteError(w, 400, err) + hh.WriteError(w, 400, err) return } - rqs, err = f.Process(rqs) + rqs, err = hh.Process(rqs) switch err { - case handlers.ErrStorage: + case errors.ErrStorage: code = 500 - case handlers.ErrEngineInit: + case errors.ErrEngineInit: code = 500 - case handlers.ErrEngineExec: + case errors.ErrEngineExec: code = 500 default: code = 200 } if code != 200 { - f.WriteError(w, 500, err) + hh.WriteError(w, 500, err) return } w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain") - rqs, err = f.Output(rqs) - rqs, perr = f.Reset(rqs) + rqs, err = hh.Output(rqs) + rqs, perr = hh.Reset(rqs) if err != nil { - f.WriteError(w, 500, err) + hh.WriteError(w, 500, err) return } if perr != nil { - f.WriteError(w, 500, perr) + hh.WriteError(w, 500, perr) return } } diff --git a/http/parse.go b/session/http/parse.go similarity index 100% rename from http/parse.go rename to session/http/parse.go diff --git a/http/server_test.go b/session/http/server_test.go similarity index 100% rename from http/server_test.go rename to session/http/server_test.go