Implementing a project

With the appropriate structure in place for our Echo project, we can get started by expanding on our simple example Echo application from Chapter 1, Understanding HTTP, Go, and Echo.

The following code is located in the $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter2/cmd/service/main.go file:

package main

import (
        "github.com/PacktPublishing/Echo-Essentials/chapter2/handlers"
        "github.com/labstack/echo"
)

func main() {
        // create a new echo instance
        e := echo.New()
        // Route / to handler function
        e.GET("/health-check", handlers.HealthCheck)
// Authentication routes e.POST("/login", handlers.Login) e.POST("/logout", handlers.Logout)
// start the server, and log if it fails
e.Logger.Fatal(e.Start(":8080")) }

Within the preceding code block, a new Echo server is instantiated, and a route is added to the path /health-check, which will be routed to the handlers.HealthCheck function. Routing will be covered for our example application later on in this chapter, and then in much greater detail in Chapter 3, Exploring Routing Capabilities, later on in the book. Finally, the server is started on port 8080 on the final line of the main function. This code imports the handler module, which has a file called $GOPATH/src/github.com/PacktPublishing/Echo-Essentials/chapter2/handlers/health_check.go, as seen in the following code:

package handlers

import (
        "net/http"

        "github.com/PacktPublishing/Echo-Essentials/plsding.me/renderings"
        "github.com/labstack/echo"
)

// HealthCheck - Health Check Handler
func HealthCheck(c echo.Context) error {
        resp := renderings.HealthCheckResponse{
                Message: "Everything is good!",
        }
        return c.JSON(http.StatusOK, resp)
}

The handlers package houses all of the API business logic in the form of Echo style handlers. As you may recall from Chapter 1, Understanding HTTP, Go, and Echo, Go's net/http package defines a handler function signature as a function that takes in an http.ResponseWriter and a *http.Request, which is very different than the handler shown in the preceding code. The handler type in Echo is a function that takes in an Echo Context, and returns an error. Within the Echo Context, we have access to the request, as well as the response writer if we wanted to access them directly. Echo also provides useful helpers methods off of the Echo Context. For example, c.JSON( in the preceding code takes the status parameter and an interface{}, and will render the provided interface{} as JSON before writing that result to the http.ResponseWriter for you!

You may also notice that we are importing the renderings package here. As mentioned previously, we designate all response structures to belong to this renderings package for ease of application development. Within the renderings directory is a file called health_check.go, which contains the following:

package renderings

type HealthCheckResponse struct {
        Message string `json:"message"`
}

This HealthCheckResponse structure contains one attribute called Message. It should be noted with this project layout structure that we can and should name all of our related files similarly to keep the code structure as intuitive as possible. Within the handlers directory, there is a health_check.go file, which will handle all of the health check-related application logic. Similarly, there is a renderings health_check.go, which will store all of the renderings related to the health check response structures.

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

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