{"action":"created","issue":{"url":"https://api.github.com/repos/zenazn/goji/issues/188","repository_url":"https://api.github.com/repos/zenazn/goji","labels_url":"https://api.github.com/repos/zenazn/goji/issues/188/labels{/name}","comments_url":"https://api.github.com/repos/zenazn/goji/issues/188/comments","events_url":"https://api.github.com/repos/zenazn/goji/issues/188/events","html_url":"https://github.com/zenazn/goji/issues/188","id":151529666,"number":188,"title":"Question: per-endpoint middleware","user":{"login":"hasryan","id":1844006,"avatar_url":"https://avatars.githubusercontent.com/u/1844006?v=3","gravatar_id":"","url":"https://api.github.com/users/hasryan","html_url":"https://github.com/hasryan","followers_url":"https://api.github.com/users/hasryan/followers","following_url":"https://api.github.com/users/hasryan/following{/other_user}","gists_url":"https://api.github.com/users/hasryan/gists{/gist_id}","starred_url":"https://api.github.com/users/hasryan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hasryan/subscriptions","organizations_url":"https://api.github.com/users/hasryan/orgs","repos_url":"https://api.github.com/users/hasryan/repos","events_url":"https://api.github.com/users/hasryan/events{/privacy}","received_events_url":"https://api.github.com/users/hasryan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-04-28T01:39:25Z","updated_at":"2016-04-28T21:52:22Z","closed_at":null,"body":"I have a question that is similar to https://github.com/zenazn/goji/issues/175 but I'm not sure it's exactly the same use case and would appreciate an opinion on my example below.\r\n\r\nI am building an API and have some endpoints that require different middleware.  The problem is that many of these endpoints are siblings, or have the same path but use a different verb, and are generally organized in ways that I can't apply the same middleware to a common prefix such as `/admin/*`.\r\n\r\nFor example, I need one series of middleware to be applied for requests to `POST /foo/bar` and another, different collection of middleware to be applied for `GET /foo/bar`.  And yet another, distinct collection of middleware applies for `DELETE /foo`.  I basically need the flexibility to apply a unique set of middleware to any given method+pattern combination.\r\n\r\nThe following appears to work, but it's not intuitive to me (providing sub-Muxes to the root Mux as if they were Handler functions, since `*Mux` implements that interface) and I would like to know if this is considered a reasonable way to solve my problem or if it's a hack.  If this isn't a reasonable way to solve it, is there another method I can use to achieve my goal?\r\n\r\n```\r\ngetFooBarMux := web.New()\r\ngetFooBarMux.Use(getFooBarMiddleware)\r\ngetFooBarMux.Get(\"/foo/bar\", getFooBarHandler)\r\n\r\npostFooBarMux := web.New()\r\npostFooBarMux.Use(postFooBarMiddleware)\r\npostfooBarMux.Post(postFooBarHandler)\r\n\r\ndeleteFooMux := web.New()\r\ndeleteFooMux.Use(deleteFooMiddleware)\r\ndeleteFooMux.Delete(deleteFooHandler)\r\n\r\nrootMux := web.New()\r\nrootMux.Get(\"/foo/bar\", getFooBarMux)\r\nrootMux.Post(\"/foo/bar\", postFooBarMux)\r\nrootMux.Delete(\"/foo\", deleteFooMux)\r\n```"},"comment":{"url":"https://api.github.com/repos/zenazn/goji/issues/comments/215573870","html_url":"https://github.com/zenazn/goji/issues/188#issuecomment-215573870","issue_url":"https://api.github.com/repos/zenazn/goji/issues/188","id":215573870,"user":{"login":"zenazn","id":12960,"avatar_url":"https://avatars.githubusercontent.com/u/12960?v=3","gravatar_id":"","url":"https://api.github.com/users/zenazn","html_url":"https://github.com/zenazn","followers_url":"https://api.github.com/users/zenazn/followers","following_url":"https://api.github.com/users/zenazn/following{/other_user}","gists_url":"https://api.github.com/users/zenazn/gists{/gist_id}","starred_url":"https://api.github.com/users/zenazn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zenazn/subscriptions","organizations_url":"https://api.github.com/users/zenazn/orgs","repos_url":"https://api.github.com/users/zenazn/repos","events_url":"https://api.github.com/users/zenazn/events{/privacy}","received_events_url":"https://api.github.com/users/zenazn/received_events","type":"User","site_admin":false},"created_at":"2016-04-28T21:52:22Z","updated_at":"2016-04-28T21:52:22Z","body":"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.\r\n\r\nAlternatively, 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.\r\n\r\nFinally, 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."}}