Middleware chaining

The Echo framework provides three mechanisms for inserting middleware into your request-processing pipeline. The Echo instance's Use(MiddlewareFunc) helper method is the most common way to add normal post-routing middleware. As you may recall, for flexibility we showed you how to use the Group(string) Echo method to organize routes. The Use(MiddlewareFunc) helper method is also available on Group instances so that you can apply middleware functions only to particular groupings of routes as well. Use tells the Echo instance, or grouping instance, that we wish to insert the middleware specified as a parameter into our middleware chain. It should be made clear that the order of your Use calls is very important. The order of the middleware insertion stipulates the order in which the middleware chain will be performed. For example, you might want two middleware, Logger and Recover, wrapping all of your routes. If you perform the following operations in order, and there is a panic within your Logger middleware , it will not be recoverable, as Recover is wrapped by Logger, and not vice versa. The following example can be found in $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter4/cmd/service/main.go:

e.Use(middleware.Logger())   // logger middleware will “wrap” recovery
e.Use(middleware.Recover()) // as it is enumerated before in the Use calls

The Echo instance also provides Pre(MiddlewareFunc) too, which allows insertion of middleware functions prior to the routing step. This is for global, non-route-specific middleware functions. An example use case for this feature would be to manipulate the request prior to routing the request within the framework. As a concrete example, you would use this feature to change the URL or target path to remove trailing slashes or perform a redirect. Another good example of a reason to use Pre-Middleware is for things like recover handling from panics within your web application, so that responses do not comprise of stack traces. The Pre function works in the same way the Use function does, in that the ordering is important to how the middleware is chained. The earlier the Pre call, the earlier the middleware starts.

The third way to insert a middleware chain is by specifying particular middleware in order as variadic parameters on the handler routing insertion calls such as GETPOSTAdd, and Any, as specified in the prior chapter. The ordering of the middleware specified in this variadic parameter is the order in which the processing will occur. It is important to note that this mechanism will only assign middleware to the specific route that is being inserted at the moment. Echo does this by composing the chaining of middleware by nesting the middleware function calls, as shown in the following code, which is also located within $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter4/cmd/service/main.go:

e.GET(“/”, HandlerFunction, Middleware1, Middleware2, Middleware3)
// RouteHandler = Middleware1(Middleware2(Middleware3(HandlerFunction)

The following is an image that describes the call stack of the middleware chain we have built:

It is important to clarify that there are four logical boundaries where middleware can be applied from an API design standpoint:

  • Global Middleware Before Routing
  • Global Middleware After Routing
  • Grouped Route Middleware
  • Route Specific Middleware

Though there are four logical boundaries to think about when using middleware applications, within the Echo implementation, there are really only two. There's middleware that is run before routing, and middleware that is applied to a processing chain that ends in the handler running. Fortunately though, Echo allows us to use the four logical boundaries, as it manages the insertion of routes and handlers into our web application. It is during this insertion, at insertion time, where Echo applies the wrapping of middleware. So, internally you only ever have two places where middleware functions are applied, before routing or after routing.

The following is an example of how Echo achieves middleware processing. You will notice that we start with a client request, which is accepted by the server. The server then kicks off the Pre-Middleware chain, which happens before routing occurs. Each middleware is run in order, and it is the responsibility of the middleware to call the next function, which is passed in as a parameter. Echo will build the function call chain and add the next middleware until there are no more in the list of middleware to be run. The Pre-Middleware, when completed, along with the chain then calls the router in order to find the handler. When the router finds the appropriate route within its search tree, the router then calls the handler function which has been wrapped by the middleware specified for that particular route. When it initiates the middleware chain associated with the particular route, it is possible at any point in the process where the middleware function is being run to return, thus causing the middleware chain to unwind backwards through the call stack until all of the middleware functions complete, resulting in a response to the caller:

It is important to note that there is nothing magical about middleware within Echo. Middleware can be boiled down to an ordered set of nested function calls of which the handler is the last nested function call.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.88.62