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

Developing Echo Projects

Of the most compelling reasons to use a web application framework for a project, the most important is that it offers clean, organized code. Frameworks offer defined structure for application development, as well as helpful functionality that minimizes the energy the developer has to use to create an application. Within an Echo project, the developer has the flexibility in implementation and project structure.

We will begin by diving into an example of an Echo project. Once we have the basic directory structure of our application defined, and the reasoning for the structure in place, we will begin to explore some fundamental building blocks and features of Echo in our example project. Firstly, we will cover target-path routing and application-handler development. Then, we will explain a high-level overview of the most commonly used features within Echo...

Technical requirements

Setting up a project in echo and organizing code

The best way to learn is by example in my opinion, so, to that end, we will walk through the initial creation of an application to best showcase the Echo framework. The source code for our sample project for this chapter is located here: https://github.com/PacktPublishing/Echo-Essentials/tree/master/chapter2. The following is a preliminary use-case diagram showing the interactions we will be creating, which will help us develop this project:

Regardless of the particulars of the project, organization of code is paramount for the success of the project. When we are developing a Go web application with Echo, it helps to start your project with a strong directory structure. A well-thought-out structure limits the number of issues with cyclical imports, and will allow you to create a clean in-project dependency tree. The organization...

Routing and handlers

When we think of routing, we often think of networking routers that send traffic on networks to their destinations. For the purposes of this book, when the term routing is used, the topic is how to map a target path to a handler function. There are many ways to do this, each with benefits and drawbacks. Sometimes, all you need is a static mapping of a target path string, such as /reminder to a function called CreateReminder. Then, any time a user visits your application with a target path of /reminder, your CreateReminder function will be triggered.

What happens, though, if you want to have a URL variable within the URL? For example, if we wanted to have /reminder/123 relate to a reminder resource with an ID of 123. It would be impractical to have all of the reminder IDs within the entire system mapped out by our target path router. You may consider performing...

Middleware

Sometimes, there are use cases where the same application logic needs to be applied to a multitude of resources within an application. A prime example of this type of logic is protecting resources behind an authentication mechanism. Some resources such as the login or static resources typically do not need this authentication logic, but other resources such as our reminder creation, snoozing, need to be protected by authentication. We can accomplish this feat with the use of middleware.

A middleware can be thought of as a wrapper for the handlers we create. An Echo middleware is defined as a function that takes the next function to call as a parameter, and returns an Echo handler. By having the parameter be the next handler function to call, we are able to build a chain of handlers:

...

Rendering

As mentioned previously, the job of a web application or an API is to create an HTTP response message, given an HTTP request message. Echo has a really nice facility to help us render responses from our handlers and middleware functions. echo.Context has methods which perform all of the heavy hitting for rendering responses back to the caller. As seen in prior examples, there is a JSON method on the Echo Context which allows for rendering of a JSON payload back to the caller. The following is a list of supported rendering capabilities within Echo:

  • HTML: Render an HTML response:

    • HTMLBlob: Render a preformed HTML response

  • JSON: Render a JSON response, converting the variable into JSON based on the struct tags of the variable:

    • JSONBlob: Render a preformed JSON response

    • JSONPretty: Render a pretty JSON response

    • JSONP: Render a JSONP response based on the struct...

Summary

Within this whirlwind chapter, much was covered about setting up an Echo project, as well as fundamentals of developing within the Echo framework. Some primary features of the framework were discussed, and examples were used to expose the reader to the concepts.

It is clear, based on the examples that were used within this chapter, that the most important aspect for a successful web application is the code's layout and organization. We are able to focus more on the particular application logic associated with our application by using the Echo framework. There is less need to find solutions to already solved problems such as request middleware pipelines, routing of URL paths to application handlers, and taking input and rendering output effectively.

In the remainder of the book, we will be looking at the features of the Echo framework in greater depth. In the next...

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}