Giter VIP home page Giter VIP logo

Comments (5)

hasryan avatar hasryan commented on May 27, 2024

To be clear, most of my handlers use similar middleware, but there are a few exceptions and that is why I am trying to achieve this level of flexibility.

from goji.

zenazn avatar zenazn commented on May 27, 2024

Would it be appropriate to inline the functionality in these specific middlewares into your handlers directly? It's hard to know for sure if that would result in an unacceptable amount of duplication, but depending on your use case it might be just as verbose as whatever solution you come up with using middleware.

Alternatively, it's pretty easy to write "inline" middleware stacks. I know several libraries that provide just this, which you might find useful. Alternatively, you can wrap the behavior you're already using above with Goji into a reusable helper to make your life a bit easier.

Finally, some middleware can easily determine (by looking at the URL or similar) if they should run on any given request. In this case, you can include each of these custom middleware in the global middleware stack, with the understanding that each layer knows when it should or should not run.

from goji.

hasryan avatar hasryan commented on May 27, 2024

@zenazn thank you for your response.

Would it be appropriate to inline the functionality in these specific middlewares into your handlers directly? It's hard to know for sure if that would result in an unacceptable amount of duplication, but depending on your use case it might be just as verbose as whatever solution you come up with using middleware.

Originally I did have the handler-specific functionality in each handler definition but it became unwieldy and I saw a lot of duplication. Also, the functionality in the middleware feels most appropriate to apply prior to invoking the handler. For instance, a few of my handlers support HTTP Basic Auth while the majority only support a custom authentication protocol.

The above example is somewhat contrived -- in reality I have about 8 common middleware that every handler uses, but a few of them have an additional handful of middleware that get added in addition to the common ones. And a few do not use the common stack of middleware at all.

Alternatively, it's pretty easy to write "inline" middleware stacks. I know several libraries that provide just this, which you might find useful.

Can you direct me to one of these so I can see how it works?


Could you also comment on whether the approach I'm taking my example is technically valid or if I've overlooked some issue? I've traced through the codebase and I don't see any issues but perhaps I'm missing something. I couldn't find any examples in the wild that are using 1 mux per route, and that makes me a little uncomfortable with this pattern.

Part of the reason I arrived at the above example is because I define all my handlers in a single slice, with each element in the slice being a struct that defines the verb, pattern, handler function, and a slice of middleware to apply. I find this method of defining my routes is aesthetically pleasing and is more condensed than using subrouters or putting conditional logic in a common set of middleware. For example:

endpoints := []endpoint{
  {
    verb: "POST",
    pattern: "/foo/bar",
    middlewares: append(commonMiddlwares, postFooBarMiddleware),
    handler: PostFooBarHandler,
  },
  {
    verb: "GET",
    pattern: "/foo/bar",
    middlewares: append(commonMiddlwares, getFooBarMiddleware),
    handler: GetFooBarHandler,
  },
  {
    verb: "DELETE",
    pattern: "/foo",
    middlewares: append(commonMiddlwares, deleteFooMiddleware),
    handler: DeleteFooHandler,
  },
}

mainMux := web.New()

for _, endpointConfig := range endpoints {  
  endpointMux := web.New()
  for _, endpointMiddleware := endpointConfigs.middlewares {
    endpointMux.Use(endpointMiddleware)
  }

  switch endpoint.verb {
  case "GET":
    endpointMux.Get(endpointConfig.pattern, endpointConfig.handler)
    mainMux.Get(endpointConfig.pattern, endpointMux)
  case "POST":
    endpointMux.Post(endpointConfig.pattern, endpointConfig.handler)
    mainMux.Post(endpointConfig.pattern, endpointMux)
  case "DELETE":
    endpointMux.Delete(endpointConfig.pattern, endpointConfig.handler)
    mainMux.Delete(endpointConfig.pattern, endpointMux)
  }
}

from goji.

zenazn avatar zenazn commented on May 27, 2024

Can you direct me to one of these so I can see how it works?

One that comes to mind is https://github.com/justinas/alice (although it is net/http-only), but you can also extract Goji's own middleware stack implementation into a standalone library if you like.

As for your code snippet at the bottom, it looks totally reasonable. It is a little unusual to use so many muxes, but given that the entire system is explicitly designed with composability in mind I see no reason why it shouldn't work well for you.

from goji.

hasryan avatar hasryan commented on May 27, 2024

Thank you, this is very helpful!

from goji.

Related Issues (20)

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.