rs/xhandler

helper function to get an xhandler.HandlerC from a context.Contex and http.Handler

Closed this issue · 3 comments

yml commented

Hello,

Thanks for this very interesting piec of code. While building a small app I add the case where I was willing to taking advantage of xaccess middleware a standard http.Handler. Adaptor is a terrible name but here it is the idea:

  // Adaptor creates a xhandler.HandlerC from an http.Handler.                                                                                                                  
  func Adaptor(ctx context.Context, h http.Handler) xhandler.HandlerC {                                                                                                         
      return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {                                                                          
          h.ServeHTTP(w, r)                                                                                                                                                     
      })                                                                                                                                                                        
  }  

and then later :

mux.Handle("/", c.HandlerCtx(ctx, Adaptor(ctx, http.HandlerFunc(stdHandlerFunc))))

If New was not already there I would have called Adaptor New and New NewC.
It is also possible that I am missing something simpler easier. :-)

rs commented

Hi @yml, thanks for your feedback.

Why can't your handler adopt the context interface?

yml commented

Because this stdHandlerFunc either come from 3rd party package or have no direct use of context.Context.

However I can see the potential of trying to unify the middleware toolchain to be homogeneous and adopt the context aware interface.

The proposed Adaptor (name is not great) help in retrofitting an existing code base and to gradually make it context aware.

rs commented

Fixed by 3107bbd with HandlerH() and HandlerF().