Adding routes

The Echo instance's Add method allows the user to insert a new route into the router. The parameters include two string arguments, method and path, as well as an echo.HandlerFunc variable for which the resulting request routing match should map. Finally, a list of applicable middleware functions to apply to the processing pipeline is the last parameter. The Add method declaration can be seen as follows:

func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {

Though there is no input validation for the method parameter within Echo RFC 7231, Section 4.3 defines allowable request methods that are acceptable. Moreover, the handler will not be mapped unless the HTTP method is one of the allowable methods within the aforementioned RFC. Go's net/http supplies a set of constants that can be used as parameters:

const (
        MethodGet     = "GET"
        MethodHead    = "HEAD"
        MethodPost    = "POST"
        MethodPut     = "PUT"
        MethodPatch   = "PATCH" // RFC 5789
        MethodDelete  = "DELETE"
        MethodConnect = "CONNECT"
        MethodOptions = "OPTIONS"
        MethodTrace   = "TRACE"
)

With this Add functionality, we are able to insert our application routes into the framework's router data structure. Though this can be comprehensive, Echo does expose a few helper methods for adding routes based on HTTP request methods. For each HTTP request method, there is a corresponding router method that wraps the Add function, which will provide a simple mechanism to map a particular method and resource target. The following are the request method helper functions that the Echo framework exposes for use:

func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route
func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route

As you can see, for every single HTTP request method that is outlined within RFC 7231 Section 4.3, there is a corresponding helper method on the Echo instance. There is also a special helper method, Any, which allows for a handler function and middleware chain to be applied to all request methods for a given path or target resource location:

func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route

Any effectively informs the Echo router that no matter what the method within the request is given for the path target resource, run this handler and use this chain of middleware functions. The intent here is for the use cases where you, as the developer, wish to perform all of the HTTP request method handling yourself, as opposed to having different handler functions for each method. It is important to note that you should not use the Any mapping assignment with any of the other method-specific assignment functions. For example, if you do the following, the Any assignment will overwrite the POST as it comes after the POST call:

e.POST(“/reminder”, handlers.CreateReminder)
e.Any(“/reminder”, handlers.Reminder)
..................Content has been hidden....................

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