Home

Awesome

🥦 Broccoli

go get -u aletheia.icu/broccoli

GoDoc Travis Go Report Card codecov.io

Broccoli uses brotli compression to embed a virtual file system of static files inside Go executables.

A few reasons to pick broccoli over the alternatives:

Performance

Admittedly, there are already many packages providing similar functionality out there in the wild. Tim Shannon did an overall pretty good overview of them in Choosing A Library to Embed Static Assets in Go, but it should be outdated by at least two years, so although we subscribe to the analysis, we cannot guarantee whether if it's up–to–date. Most if not all of the packages mentioned in the article, rely on gzip compression and most of them, unfortunately are not compatible with wasm/js architecture, due to some quirk that has to do with their use of http package. This, among other things, was the driving force behind the creation of broccoli.

The most feature-complete library from the comparison table seems to be fileb0x.

How does broccoli compare to fileb0x?

Featurefileb0xbroccoli
compressiongzipbrotli (-20% avg.)
optional decompressionyesyes
compression levelsyesyes (1-11)
different build tags for each fileyesno
exclude / ignore filesglobglob
unexported vars/funcsoptionaloptional
virtual memory file systemyesyes
http file systemyesyes
replace text in filesyesno
glob supportyesyes
regex supportnono
config fileyesno
update files remotelyyesno
.gitignore supportnoyes

How does it compare to others?

Broccoli seems to outperform the existing solutions.

We did benchmarks, please feel free to review them and correct us whenever our methodology could be flawed.

Usage

$ broccoli
Usage: broccoli [options]

Broccoli uses brotli compression to embed a virtual file system in Go executables.

Options:
	-src folder[,file,file2]
		The input files and directories, "public" by default.
	-o
		Name of the generated file, follows input by default.
	-var=br
		Name of the exposed variable, "br" by default.
	-include *.html,*.css
		Wildcard for the files to include, no default.
	-exclude *.wasm
		Wildcard for the files to exclude, no default.
	-opt
		Optional decompression: if enabled, files will only be decompressed
		on the first time they are read.
	-gitignore
		Enables .gitignore rules parsing in each directory, disabled by default.
	-quality [level]
		Brotli compression level (1-11), the highest by default.

Generate a broccoli.gen.go file with the variable broccoli:
	//go:generate broccoli -src assets -o broccoli -var broccoli

Generate a regular public.gen.go file, but include all *.wasm files:
	//go:generate broccoli -src public -include="*.wasm"

How broccoli is used in the user code:

//go:generate broccoli -src=public,others -o assets

func init() {
    br.Walk("public", func(path string, info os.FileInfo, err error) error {
        // walk...
        return nil
    })
}

func main() {
    http.ListenAndServe(":8080", br.Serve("public"))
}

Credits

License: MIT

We would like to thank brotli development team from Google and Andy Balholm, for his c2go pure-Go port of the library. Broccoli itself is an effort of a mentoring experiment, lead by @tucnak on the foundation of Aletheia.