Serving static files

When we talk about static files, we are talking about web application assets that are needed in order to render a working user experience. Static file assets include, but are not limited to, Cascading Style Sheet (CSS) files, JavaScript (JS) files, and any other file that a user would need to download in order to render the web application within the browser. Within Echo, we are able to present an interface for the browser to download these assets much in the same way we present new routes within Echo. The following is an example from our project where we are going to present a route called /static/ wherein every request that is made to any resource prefixed with /static/ will be served from a particular directory which is based on the location of the binary executable, in our example the ./static/ directory:

e.Static("/static", "static")

There are a lot of things happening with this one line, so I will break it down. The e.Static method takes two arguments, a route path expression, which in this case is /static, as well as a location on the filesystem, static. This location on the filesystem must be bound to a directory that is within the current working directory. The route path expression employs Echo's wildcard matching capabilities that we covered in the chapter on routing. In essence, this tells Echo that any request that is prefixed with /static/ will be handled by this static handling functionality.

Of interest, within the implementation in Echo there is also a mechanism by which a developer can specify how to serve just a single file as a particular route. For example, if you wanted to serve a file ./static/index.html whenever a user requested the URL / , you could add the following:

e.File("/", "static/index.html")

With this line we are telling Echo that any request for / should be responded to with the contents of the file located in ./static/index.html. As you can see, this feature allows for flexibility in how the rendering of static content is performed. 

Echo exposes Static method below, which allows us to serve static content on a directory level. As you can see in the following, the Static method takes in a prefix, and a root directory to serve files from:

// Static registers a new route with path prefix to serve static files from the
// provided root directory.
func (e *Echo) Static(prefix, root string) *Route {
        if root == "" {
                root = "." // For security we want to restrict to CWD.
        }
        return static(e, prefix, root)
}

It is helpful to note that this code will default the root path to the current working directory in the event you pass in an empty string to the Static method. Following is how the static function implements the routing and serving of static content:

func static(i i, prefix, root string) *Route {
        h := func(c Context) error {
                p, err := url.PathUnescape(c.Param("*"))
                if err != nil {
                        return err
                }
                name := filepath.Join(root, path.Clean("/"+p)) // "/"+ for security
                return c.File(name)
        }
        i.GET(prefix, h)
        if prefix == "/" {
                return i.GET(prefix+"*", h)
        }

        return i.GET(prefix+"/*", h)
}

The static function returns an *echo.Route which is a route to a very simple handler. Within the definition above, the handler created first takes the wildcard parameter and performs a URL encoding path unescape on the parameter. In our case this wildcard parameter is going to be the file name of the asset we wish to serve to the caller. We then generate a file name relative to the root that was defined in the Static method call. After we have created a file name from the root of our content directory and the request URI, Echo then performs a c.File call to return the file content from the web server. Near the end of this function you can see we are registering this handler for the static assets to the prefix+"/*" resource path location.

Clearly, static content delivery builds on top of the file-serving capabilities that are built into Echo. This functionality allows a developer to package up file content and allow that content to be served to callers without having to reimplement a file-serving handler themselves. This feature allows developers to package up static content such as HTML, JavaScript and CSS content within the Go deployment artifact directly.

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

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