Giter Site home page Giter Site logo

xhandler's People

Contributors

achiku avatar blang avatar deepanchor avatar kevinconway avatar rs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xhandler's Issues

Composable stacks of middleware?

Hey,

Thanks for this awesome project!

I was wondering what your thoughts would be about adding support for composable stacks of middleware, kind of like this project.

It can already be quite verbose calling Use and UseC for every piece of middleware that you want to incorporate, but when you start having different middleware chains for different routes it can get a bit repetitive.

I'm thinking something like:

std := xhandler.Chain{}
std.UseStack(close, timeout, logger, stats) // alternatively UseS?
...
auth := std.Append(jwt, cors)

would be a pretty nice convenience to have.

To add middleware that are not context aware, maybe we could do something similar to the project above, such as:

std.UseStack(close, timeout, logger, xhandler.Adapt(cors, jwt))

Please tag and version this project

Tags and versions aid in packaging for things like .deb files, and for anyone wanting to use Glide and other version based package managers to version their dependencies. It would also help ensure that if you make a breaking change, we don't pull it down until we are ready.

Xmux moved to its own repository

If you started testing xmux, please change your imports from github.com/rs/xhandler/xmux to github.com/rs/xmux.

You may want to move your stars as well if you like it ;)

Sorry for the inconvenience.

Protected handlers

Is there an idiomatic way to protect handlers in xhandler or xmux?

There's a handful of ways: wrapping each handler when added to muxer, multiple muxers with different chains, route groups with different chains, using xhandler.If, etc.

Curious if something obvious stands out to you @rs before I just wrap handlers as necessary....

Question: How to set a middleware on a xmux.Group?

		c := xhandler.Chain{}
		c.UseC(xhandler.CloseHandler)
		c.UseC(xhandler.TimeoutHandler(2 * time.Second))
		mux := xmux.New()
		iot := mux.NewGroup("/iot")
		iot.GET("/welcome/:name", Middleware)
		root := mux.NewGroup("/pip")
		root.GET("/:pip", Middleware)
                // Is there a way to do this? 
                root.Use(CustomMiddleware)
		http.ListenAndServe(":9000", xhandler.New(context.Background(), mux))

Have requests use the xhandler context

I see the comments in #14 that suggest switching to httprouter after go1.7 because the xhandler features are redundant. I think that's a good way to go, but, given that it takes time to refactor code, I'm wondering if we can make the transition a little easier.

In my case, I have several things running that leverage xhandler and I want to upgrade to go1.7. After the upgrade the rest of the x-libs automatically begin leveraging the new Request.Context() for storage and provide new FromRequest functions to fetch values. Unfortunately, other code is still using the context given from xhandler which means I have two context objects with different values in them. Any code path that accepts a context object now needs to know if it should be the xhandler context or the request context. If the code needs something from both then I'm broken.

I believe a way to resolve this is to make the New() function set the request context to the one generated by xhandler with r.WithContext(ctx). This should not impact any existing functionality while also making the 1.7 upgrade possible without large code refactors. I'll post a PR to illustrate.

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

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. :-)

What is the best way to have root context with xhandler?

Hi, this library looks great, and thank you for open sourcing it! I have a question regarding to so-called root context, which contains values used in any part of existing service (e.g. database connection, or application config).

README and examples describe how to handle request scope context, but I couldn't find a way to integrate root context with application. It could be done by adding a middleware to initialize root context, and put it in the first position of middleware chain, but this method is not really clean since it has to check if key exists in every request.

I might have completely misunderstood what the library provides (maybe it's focusing only on request scope context?), but appreciate any comment. Thaks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.