Static files in Go

Datetime:2016-08-23 05:15:27          Topic: Golang           Share

Aug 2016

A common problem when developing a Go-based web service is that of static files. While you could rsync over your files before deploying a new binary, I prefer packing it into the binary for easier deployment and atomicity of the assets. There are quite a few packages out there already to help you compile assets into the binary, most notably the following:

None of these tools were quite there for me, and were missing some feature that I really wanted. The most important thing for me is that I wanted the assets to not just be compressed before being compiled into the binary, but to also be served compressed, so the server doesn’t need to do an unnecessary decompression (and possibly compress it again before sending it to the client).

Staticfiles

So, to scratch my own itch I made Staticfiles , a command line tool that’s very similar to the tools listed above, but different in some key ways. Most notably, it doesn’t constrain itself by implementing the http.FileSystem API, which makes it possible to do things like serving files directly in their compressed form. It also doesn’t have a built-in development mode, but because of its support for build tags it is very easy to extend it with one (and I have an example of how to do this in the repo). I also made sure it’s very fast, by using ego for the template generation and parallel reading and writing of files.

Check it out

Please try out Staticfiles and let me know what you think! If you run into a problem, feel free to open an issue and I can take a look.

You should follow me on Twitter !





About List