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,
}, nil
}

View File

@ -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.

View File

@ -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
}

View File

@ -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)