Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Echo Quick Start Guide

You're reading from  Echo Quick Start Guide

Product type Book
Published in May 2018
Publisher Packt
ISBN-13 9781789139433
Pages 136 pages
Edition 1st Edition
Languages
Author (1):
Ben Huson Ben Huson
Profile icon Ben Huson

Exploring Routing Capabilities

In a web application, routing is the process of mapping the path component of a Uniform Resource Identifier (URI) to the handler code that represents a given resource. There are many ways to implement this functionality, all with positive and negative consequences for performance, usability, and functionality. The Echo framework provides an efficient solution to this problem through the implementation of a Radix Tree. Other frameworks range in routing implementation by using regular expressions, maps, and other mechanisms to perform this mapping functionality.

Within this chapter, we will go through examples of how the routing capabilities within the Echo framework perform better than many other web application frameworks. We will dive into route organization, grouping of routes, and real examples of best practices when coming up with your routing...

Technical requirements

Basic handler routing

As you may recall, a URI path component determines the resource representation for a web application. Within the HTTP request message structure, not only do we have a target in the request line, but we also have the requested method as well. HTTP methods, or verbs, explain the intent of the request to act on the resource identified by the target. Within the realm of application development, the request method, in conjunction with the target resource identifier, will determine what application code the service is supposed to run. To this end, we will start by talking about how to add route mappings to your Echo project, and more importantly, how Echo uses the information in the request to run your handlers.

Adding routes

...

Group routing

Alluded to earlier, there exists a grouping capability for defined routes within the Echo framework. Grouping of routes allows for simple logical groupings of sets of resources with a base prefix. A great example that works well with grouped routes is when you wish to have a target resource path versioned API, such as providing /v1/login and /v1/logout, as well as /v2/login and /v2/logout. This happens to be a very common way in which developers can prevent breakage of their API from major changes to the API. Typically, if the schema of the request or response changes, it is a best practice to create a new version of those resource targets. This helps identify to developers integrating with your API that there is a breaking change between the two APIs.

In this example, we would create a /v1/ and a /v2 group with the Group method, which is declared in the following...

Router implementation considerations

If you perform a search for Go Frameworks, you will come across a variety of different solutions, all with slightly different routing implementations. Luckily, based on https://github.com/julienschmidt/go-http-routing-benchmark, we have a mechanism by which we can pit all of these routers against each other to see which performs the best and which performs the worst under certain situations. Using these benchmarks when I was initially investigating router capabilities, the benchmarks provided gave some excellent insight into the efficiencies of the various routers from 2015:

BenchmarkAce_GithubAll 93675 ns/op 167 allocs/op
BenchmarkBear_GithubAll 264194 ns/op 943 allocs/op
BenchmarkBeego_GithubAll 1109160 ns/op 2092 allocs/op
BenchmarkBone_GithubAll 2063973 ns/op 8119 allocs/op
BenchmarkDenco_GithubAll 83114 ns/op 167 allocs/op
BenchmarkEcho_GithubAll...

In action

When implementing routing for your API or web application within the Echo Framework, it is important to keep routes logically separated with grouping, used for functional differences in the application, and/or versioning purposes. It is important to remember how the router implements the mapping, and in Echo’s case since Echo uses a Radix Tree, the more similar prefixes you use within your API definition, the better the routing performance will be for your application. This is important to note in your API URL design because it will have important performance ramifications.

One item that has been left out of our routing discussion so far is how Echo handles URL parameters. You use a URL parameter when you need to have a variable length argument for your API located within the target path itself. This is very useful in API design, as you may want to reference a...

Summary

Resource target routing is a complicated problem that can be solved in many different ways. When creating a web application with Echo, you can rest assured that the routing will allow you the flexibility you need to nest your logical groupings of routes. You can also take solace in the fact that the overhead related to the routing of your API will be very low, both in performance and allocations per request.

Within the next chapter, we will discover the benefits of having a built-in middleware chaining solution within the Echo framework. We will also take a look at all of the excellent contributed middleware functions that are included with the web framework that will help with logging, error handling, and authentication and authorization mechanisms.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Echo Quick Start Guide
Published in: May 2018 Publisher: Packt ISBN-13: 9781789139433
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}