Bouke van der Bijl

Static files in Go

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).


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). The generated file server will add ETag and Last-Modified headers to the responses, to avoid unnecessary file downloads. I also made sure the tool itself is very fast, by parallelizing reading and compressing 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.

Aug 2016