This commit is contained in:
r8zavetr8v 2025-01-02 15:33:15 -08:00
parent f44813431d
commit d0f10cec62
4 changed files with 27 additions and 8 deletions

View File

@ -118,3 +118,4 @@ func (d *RegisterHandler) register(
Ok: true, Ok: true,
}, nil }, nil
} }

View File

@ -45,13 +45,16 @@ func NewRequestPool() *RequestPool {
} }
type Request struct { type Request struct {
ID string ID string
Session *models.Session Session *models.Session
User *models.User User *models.User
// ResolveValues - data required to process request.
ResolveValues *sync.Map ResolveValues *sync.Map
Metadata *sync.Map // Metadata - an additional data, usually added with preprocessing.
Body []byte Metadata *sync.Map
RawReq *http.Request // Request body
Body []byte
RawReq *http.Request
} }
// NewRequestFromHttp builds a new *Request struct from raw http Request. No auth data validated. // NewRequestFromHttp builds a new *Request struct from raw http Request. No auth data validated.

View File

@ -15,12 +15,14 @@ type Handler interface {
GetName() string GetName() string
GetRequiredResolveParams() []string GetRequiredResolveParams() []string
GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error 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 { type BaseHandler struct {
Name string Name string
RequiredResolveParams []string RequiredResolveParams []string
ProcessFn func(ctx context.Context, req *common.Request, w Writer) error 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 { func New() *BaseHandler {
@ -42,6 +44,11 @@ func (h *BaseHandler) WithProcessFunc(fn func(ctx context.Context, req *common.R
return h 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 { func (h *BaseHandler) GetName() string {
return h.Name 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 { func (h *BaseHandler) GetProcessFn() func(ctx context.Context, req *common.Request, w Writer) error {
return h.ProcessFn return h.ProcessFn
} }
func (h *BaseHandler) GetPreprocessFn() func(ctx context.Context, req *common.Request, w Writer) error {
return h.PreprocessFn
}

View File

@ -47,8 +47,12 @@ func (p *GinProcessor) Process(handler handler.Handler) gin.HandlerFunc {
return 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 // 4. Call handler.ProcessFn
if err = handler.GetProcessFn()(ctx, req, wrapGin(ctx)); err != nil { if err = handler.GetProcessFn()(ctx, req, wrapGin(ctx)); err != nil {
p.writeError(ctx, err) p.writeError(ctx, err)