{"action":"opened","issue":{"url":"https://api.github.com/repos/kataras/iris/issues/298","repository_url":"https://api.github.com/repos/kataras/iris","labels_url":"https://api.github.com/repos/kataras/iris/issues/298/labels{/name}","comments_url":"https://api.github.com/repos/kataras/iris/issues/298/comments","events_url":"https://api.github.com/repos/kataras/iris/issues/298/events","html_url":"https://github.com/kataras/iris/issues/298","id":166163787,"number":298,"title":"Context.After(middleware) or something similar - execute middleware after handler","user":{"login":"cristian-sima","id":12199042,"avatar_url":"https://avatars.githubusercontent.com/u/12199042?v=3","gravatar_id":"","url":"https://api.github.com/users/cristian-sima","html_url":"https://github.com/cristian-sima","followers_url":"https://api.github.com/users/cristian-sima/followers","following_url":"https://api.github.com/users/cristian-sima/following{/other_user}","gists_url":"https://api.github.com/users/cristian-sima/gists{/gist_id}","starred_url":"https://api.github.com/users/cristian-sima/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cristian-sima/subscriptions","organizations_url":"https://api.github.com/users/cristian-sima/orgs","repos_url":"https://api.github.com/users/cristian-sima/repos","events_url":"https://api.github.com/users/cristian-sima/events{/privacy}","received_events_url":"https://api.github.com/users/cristian-sima/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-07-18T18:44:13Z","updated_at":"2016-07-18T18:44:13Z","closed_at":null,"body":"Hi.\r\n\r\nThe idea is simple. I have multiple companies, and each one has invoices. So, I have a `iris.Party` for each company and a `iris.MuxAPI` for the invoices. \r\n\r\nBefore I request or update the invoices, I want to connect to 2 databases, check the user has the right for that company and check if the account is login. This is done by my `ConnectToCompanyDatabase` array which contains an array of middlewares.\r\n\r\nThis is the code for what I've described:\r\n\r\n```\r\nfunc LoadAPI(api iris.MuxAPI) {\r\n\tcube := api.Party(\"/cube/:company\", middleware.ConnectToCompanyDatabase...)\r\n\t{\r\n\t\tcube.API(\"invoices\", invoiceAPI{})\r\n\t}\r\n}\r\n```\r\n\r\nGood. *That works*\r\n\r\nThe problem which appears is that, **after** I've done my requests and I've finished with the handler, I am required to close the connections to the databases. So, the only option is to add the middle ware to do this, after each request. For instance: \r\n\r\n```\r\nfunc (api invoiceAPI) Get() {\r\n\r\n\t// ... here I generate the response\r\n\r\n\tcontext.JSON(200, response)\r\n\r\n\tmiddleware.CloseDatabaseConnections(context)\r\n}\r\n```\r\n\r\nAs you can see, I have to repeat this for each handler: **add, delete, update** and so on. The same problem is if let's say a company has `invoices` and `clients`. So, you got the idea, after each handler I have to repeat the code. Also, the problem is that *I may omit the code*.\r\n\r\nTherefore, I need a special function which I can call *after the handler has been called* inside an `iris.API` or `iris.Party`. Or another solution can be to insert the closing database's middleare at the definition of the `API` or `Party` in a way. Here is a idea:\r\n\r\n```\r\nfunc LoadAPI(api iris.MuxAPI) {\r\n\tcube := api.Party(\"/cube/:company\", middleware.ConnectToCompanyDatabase...).Finish(middleware.CloseDatabaseConnections)\r\n\t{\r\n\t\tcube.API(\"invoices\", invoiceAPI{})\r\n\t}\r\n}\r\n```\r\n\r\nWhat do you think?"}}
{"actor":{"display_login":"cristian-sima"}}