From d0f10cec62b7894533277b8d52e42619ec89b0fb Mon Sep 17 00:00:00 2001 From: optclblast Date: Thu, 2 Jan 2025 15:33:15 -0800 Subject: [PATCH] +1 --- internal/app/handlers/register.go | 1 + internal/common/request.go | 15 +++++++++------ internal/handler/handler.go | 11 +++++++++++ internal/processor/gin_processor.go | 8 ++++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/internal/app/handlers/register.go b/internal/app/handlers/register.go index d35aba6..dcae355 100644 --- a/internal/app/handlers/register.go +++ b/internal/app/handlers/register.go @@ -118,3 +118,4 @@ func (d *RegisterHandler) register( Ok: true, }, nil } + diff --git a/internal/common/request.go b/internal/common/request.go index aab27ed..b7bda24 100644 --- a/internal/common/request.go +++ b/internal/common/request.go @@ -45,13 +45,16 @@ func NewRequestPool() *RequestPool { } type Request struct { - ID string - Session *models.Session - User *models.User + ID string + Session *models.Session + User *models.User + // ResolveValues - data required to process request. ResolveValues *sync.Map - Metadata *sync.Map - Body []byte - RawReq *http.Request + // Metadata - an additional data, usually added with preprocessing. + Metadata *sync.Map + // Request body + Body []byte + RawReq *http.Request } // NewRequestFromHttp builds a new *Request struct from raw http Request. No auth data validated. diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 73c8c65..716e865 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -15,12 +15,14 @@ type Handler interface { GetName() string GetRequiredResolveParams() []string GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error + GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error } type BaseHandler struct { Name string RequiredResolveParams []string ProcessFn func(ctx context.Context, req *common.Request, w Writer) error + PreprocessFn func(ctx context.Context, req *common.Request, w Writer) error } func New() *BaseHandler { @@ -42,6 +44,11 @@ func (h *BaseHandler) WithProcessFunc(fn func(ctx context.Context, req *common.R return h } +func (h *BaseHandler) WithPreprocessFunc(fn func(ctx context.Context, req *common.Request, w Writer) error) *BaseHandler { + h.PreprocessFn = fn + return h +} + func (h *BaseHandler) GetName() string { return h.Name } @@ -53,3 +60,7 @@ func (h *BaseHandler) GetRequiredResolveParams() []string { func (h *BaseHandler) GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error { return h.ProcessFn } + +func (h *BaseHandler) GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error { + return h.PreprocessFn +} diff --git a/internal/processor/gin_processor.go b/internal/processor/gin_processor.go index 6a2aff1..b6433e9 100644 --- a/internal/processor/gin_processor.go +++ b/internal/processor/gin_processor.go @@ -47,8 +47,12 @@ func (p *GinProcessor) Process(handler handler.Handler) gin.HandlerFunc { return } - // 3. Call preprocessinf fn's, middlewares etc. - // .... + // 3. Call preprocessing fn's, middlewares etc. + if err = handler.GetPreprocessFn()(ctx, req, wrapGin(ctx)); err != nil { + p.writeError(ctx, err) + return + } + // 4. Call handler.ProcessFn if err = handler.GetProcessFn()(ctx, req, wrapGin(ctx)); err != nil { p.writeError(ctx, err)