diff --git a/README.md b/README.md index 7ebe1d6f..83a9d687 100644 --- a/README.md +++ b/README.md @@ -41,36 +41,36 @@ Iris may have reached version 8, but we're not stopping there. We have many feat * [Latest changes](https://github.com/kataras/iris/blob/master/HISTORY.md#su-27-august-2017--v840) * [Learn](#-learn) * [HTTP Listening](_examples/#http-listening) - * [Configuration](_examples/#configuration) - * [Routing, Grouping, Dynamic Path Parameters, "Macros" and Custom Context](_examples/#routing-grouping-dynamic-path-parameters-macros-and-custom-context) - * [MVC (Model View Controller)](_examples/#mvc) **NEW** - * [Subdomains](_examples/#subdomains) - * [Wrap `http.Handler/HandlerFunc`](_examples/#convert-httphandlerhandlerfunc) - * [View](_examples/#view) - * [Authentication](_examples/#authentication) - * [File Server](_examples/#file-server) - * [How to Read from `context.Request() *http.Request`](_examples/#how-to-read-from-contextrequest-httprequest) - * [How to Write to `context.ResponseWriter() http.ResponseWriter`](_examples/#how-to-write-to-contextresponsewriter-httpresponsewriter) - * [Test](_examples/#testing) - * [Cache](_examples/#caching) - * [Sessions](_examples/#sessions) - * [Websockets](_examples/#websockets) - * [Miscellaneous](_examples/#miscellaneous) - * [POC: Convert the medium-sized project "Parrot" from native to Iris](https://github.com/iris-contrib/parrot) - * [Typescript Automation Tools](typescript/#table-of-contents) - * [Tutorial: Online Visitors](_examples/tutorial/online-visitors) - * [Tutorial: Caddy](_examples/tutorial/caddy) + * [Configuration](_examples/#configuration) + * [Routing, Grouping, Dynamic Path Parameters, "Macros" and Custom Context](_examples/#routing-grouping-dynamic-path-parameters-macros-and-custom-context) + * [MVC (Model View Controller)](_examples/#mvc) **NEW** + * [Subdomains](_examples/#subdomains) + * [Wrap `http.Handler/HandlerFunc`](_examples/#convert-httphandlerhandlerfunc) + * [View](_examples/#view) + * [Authentication](_examples/#authentication) + * [File Server](_examples/#file-server) + * [How to Read from `context.Request() *http.Request`](_examples/#how-to-read-from-contextrequest-httprequest) + * [How to Write to `context.ResponseWriter() http.ResponseWriter`](_examples/#how-to-write-to-contextresponsewriter-httpresponsewriter) + * [Test](_examples/#testing) + * [Cache](_examples/#caching) + * [Sessions](_examples/#sessions) + * [Websockets](_examples/#websockets) + * [Miscellaneous](_examples/#miscellaneous) + * [POC: Convert the medium-sized project "Parrot" from native to Iris](https://github.com/iris-contrib/parrot) + * [Typescript Automation Tools](typescript/#table-of-contents) + * [Tutorial: Online Visitors](_examples/tutorial/online-visitors) + * [Tutorial: Caddy](_examples/tutorial/caddy) * [Middleware](middleware/) * [Dockerize](https://github.com/iris-contrib/cloud-native-go) * [Community & Support](#-community) * [Blogs](https://iris-go.com/v8/blogs) - - [Iris Go vs .NET Core Kestrel in terms of HTTP performance](https://hackernoon.com/iris-go-vs-net-core-kestrel-in-terms-of-http-performance-806195dc93d5) - - [Go vs .NET Core in terms of HTTP performance](https://medium.com/@kataras/go-vs-net-core-in-terms-of-http-performance-7535a61b67b8) - - [Iris, a modular web framework](https://medium.com/@corebreaker/iris-web-cd684b4685c7) - - [Deploying a Iris Golang app in hasura](https://docs.hasura.io/0.14/tutorials/deploying-a-go-iris-app.html) - - [How to Turn an Android Device into a Web Server](https://twitter.com/ThePracticalDev/status/892022594031017988) - - [A URL Shortener Service using Go, Iris and Bolt](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7) - - [Why I preferred Go over Node.js for simple Web Application](https://medium.com/@tigranbs/why-i-preferred-go-over-node-js-for-simple-web-application-d4a549e979b9) + - [Iris Go vs .NET Core Kestrel in terms of HTTP performance](https://hackernoon.com/iris-go-vs-net-core-kestrel-in-terms-of-http-performance-806195dc93d5) + - [Go vs .NET Core in terms of HTTP performance](https://medium.com/@kataras/go-vs-net-core-in-terms-of-http-performance-7535a61b67b8) + - [Iris, a modular web framework](https://medium.com/@corebreaker/iris-web-cd684b4685c7) + - [Deploying a Iris Golang app in hasura](https://docs.hasura.io/0.14/tutorials/deploying-a-go-iris-app.html) + - [How to Turn an Android Device into a Web Server](https://twitter.com/ThePracticalDev/status/892022594031017988) + - [A URL Shortener Service using Go, Iris and Bolt](https://medium.com/@kataras/a-url-shortener-service-using-go-iris-and-bolt-4182f0b00ae7) + - [Why I preferred Go over Node.js for simple Web Application](https://medium.com/@tigranbs/why-i-preferred-go-over-node-js-for-simple-web-application-d4a549e979b9) * [Versioning](#-version) * [People](#-people) diff --git a/_examples/README.md b/_examples/README.md index f14368e1..e3b47b5e 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -90,6 +90,9 @@ Navigate through examples for a better understanding. * [method overriding](routing/custom-context/method-overriding/main.go) * [new implementation](routing/custom-context/new-implementation/main.go) - [Route State](routing/route-state/main.go) +- [Writing a middleware](routing/writing-a-middleware) + * [per-route](routing/writing-a-middleware/per-route/main.go) + * [globally](routing/writing-a-middleware/globally/main.go) ### MVC diff --git a/_examples/routing/writing-a-middleware/globally/main.go b/_examples/routing/writing-a-middleware/globally/main.go new file mode 100644 index 00000000..2093d854 --- /dev/null +++ b/_examples/routing/writing-a-middleware/globally/main.go @@ -0,0 +1,56 @@ +package main + +import "github.com/kataras/iris" + +func main() { + app := iris.New() + // register the "before" handler as the first handler which will be executed + // on all domain's routes. + // or use the `UseGlobal` to register a middleware which will fire across subdomains. + app.Use(before) + // register the "after" handler as the last handler which will be executed + // after all domain's routes' handler(s). + app.Done(after) + + // register our routes. + app.Get("/", indexHandler) + app.Get("/contact", contactHandler) + + app.Run(iris.Addr(":8080")) +} + +func before(ctx iris.Context) { + shareInformation := "this is a sharable information between handlers" + + requestPath := ctx.Path() + println("Before the indexHandler or contactHandler: " + requestPath) + + ctx.Values().Set("info", shareInformation) + ctx.Next() +} + +func after(ctx iris.Context) { + println("After the indexHandler or contactHandler") +} + +func indexHandler(ctx iris.Context) { + println("Inside indexHandler") + + // take the info from the "before" handler. + info := ctx.Values().GetString("info") + + // write something to the client as a response. + ctx.HTML("