• This repository has been archived on 06/Jan/2021
  • Stars
    star
    505
  • Rank 87,346 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 8 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

staticfiles compiles a directory of files into an embeddable .go file

DEPRECATED

Go 1.16 has file embedding built-in, you should use that!

staticfiles

Staticfiles allows you to embed a directory of files into your Go binary. It is optimized for performance and file size, and automatically compresses everything before embedding it. Here are some of its features:

  • Compresses files, to make sure the resulting binary isn't bloated. It only compresses files that are actually smaller when gzipped.
  • Serves files gzipped (while still allowing clients that don't support it to be served).
  • Ignores hidden files (anything that starts with .).
  • Fast. The command-line tool reads and compresses files in parallel, and the resulting Go file serves files very quickly, avoiding unnecessary allocations.
  • No built-in development mode, but makes it very easy to implement one (see local development mode).

It has some clever tricks, like only compressing a file if it actually makes the binary smaller (PNG files won't be compressed, as they already are and compressing them again will make them bigger).

I recommend creating a separate package inside your project to serve as the container for the embedded files.

Example

For an example of how to use the resulting package, check out example/example.go. You can also see the API it generates at godoc.org.

Installation

Install with

go get bou.ke/staticfiles

Usage

Simply run the following command (it will create the result directory if it doesn't exist yet):

staticfiles -o files/files.go static/

I recommend putting it into a Makefile as follows:

files/files.go: static/*
	staticfiles -o files/files.go static/

The staticfiles command accept the following arguments:

--build-tags string
      Build tags to write to the file
-o string
      File to write results to. (default "staticfiles.go")
--package string
      Package name of the resulting file. Defaults to name of the resulting file directory

Local development mode

While Staticfiles doesn't have a built-in local development mode, it does support build tags which makes implementing one very easy. Simply run staticfiles with --build-tags="!dev" and add a file in the same directory that implements the same API, but with //+build dev at the that and using http.FileServer under the hood. You can find an example in files/files_dev.go. Once you have that set up you can simply do go build --tags="dev" to compile the development version. In the way I set it up, you could even do go build --tags="dev" -ldflags="-X bou.ke/staticfiles/files.staticDir=$(pwd)/static" to set the static file directory to a specific path.

API

The resulting file will contain the following functions and variables:

func ServeHTTP(http.ResponseWriter, *http.Request)

ServeHTTP will attempt to serve an embedded file, responding with gzip compression if the clients supports it and the embedded file is compressed.

func Open(name string) (io.ReadCloser, error)

Open allows you to read an embedded file directly. It will return a decompressing Reader if the file is embedded in compressed format. You should close the Reader after you're done with it.

func ModTime(name string) time.Time

ModTime returns the modification time of the original file. This can be useful for caching purposes.

NotFound http.Handler

NotFound is used to respond to a request when no file was found that matches the request. It defaults to http.NotFound, but can be overwritten.

Server http.Handler

Server is simply ServeHTTP but wrapped in http.HandlerFunc so it can be passed into net/http functions directly.

More Repositories

1

monkey

Monkey patching in Go
Go
3,235
star
2

dark-mode-notify

Run a script whenever dark mode changes in macOS
Swift
190
star
3

extractdata

Live at http://extractdata.club
Go
169
star
4

babelfish

Translate bash scripts to fish
Go
166
star
5

gonerics

Generics for go
Go
114
star
6

statictemplate

Statictemplate is a code generator for Go's text/template and html/template.
Go
76
star
7

go-faster

You can always Go faster
Go
49
star
8

HTTPS4All

Rewrite HTTP hosts to HTTPS whenever possible
Swift
35
star
9

b

My setup
Nix
30
star
10

kubectl-dashboard

Instantly get a Kubernetes dashboard
Go
15
star
11

https-everywhere-host-list

Tool to convert HTTPS Everywhere rules to a list of hosts that support HTTPS.
HTML
12
star
12

arduino-nix

Manage your arduino-cli with Nix
Nix
12
star
13

memorizationbot

Source code for https://memorizationbot.com/
Go
10
star
14

deoplete-markdown-links

Complete wiki links and tags in Markdown.
Python
7
star
15

esbuild-ruby

Ruby
6
star
16

unsafer

This is a bad idea
Go
4
star
17

proxy-now

Use Tasker to add custom voice commands to Google Now
Java
4
star
18

gameboy-emu

Go
3
star
19

go-lisp

A lisp interpreter in go
Go
3
star
20

objfile

Go
3
star
21

splendimax

Splendor AI that uses minimax to play
Rust
3
star
22

elfie

Package elfie takes an 'ELF-selfie' of the current process.
Go
3
star
23

file2const

Generates a Go file containing the given files as strings
Go
3
star
24

rust-sysv-ipc

Implementation of Sysv IPC for Rust
Rust
2
star
25

symme

Retrieve the symbol table for the current process
Go
2
star
26

boggle-solver

Show all possible words given a boggle board
C++
2
star
27

procmaps

Package procmaps provides a parser for the /proc maps format
Go
2
star
28

esbuilder

Integrate esbuild into Rails
Ruby
1
star
29

AprilTags

Apriltags bindings for Swift
Swift
1
star
30

uva

My various uva solutions
C++
1
star
31

whoamits

Service that tells you who you are based on Tailscale
Go
1
star
32

hyves-stats

Track Hyves' users' stats
PHP
1
star
33

s3redirect

Just redirects every request to a presigned URL of a certain bucket.
Go
1
star
34

cross-chain-nix

Binutils cross-compilation toolchains for nix
Nix
1
star
35

tempdb

Go
1
star
36

orm

An ORM for Go that's good. WIP
Go
1
star
37

gotre

Go Tail Recursion Eliminator
Go
1
star
38

rayray

Rust
1
star
39

putio

This is a webOS application that immediately redirects to tv.put.io
HTML
1
star
40

fast-postgres

Fast postgres Docker image for tests
1
star
41

motion-pong

Motion controlled pong game for school project
Python
1
star
42

ctxdb

ctxdb provides access to the database through a Context
Go
1
star
43

webgl-sand

A falling sand game completely implemented in webgl
JavaScript
1
star
44

tools

Misc command line utilities
Go
1
star
45

skype-db

Ruby
1
star
46

go-template-parser

Go
1
star
47

txdriver

txdriver provides transaction isolation for Go SQL tests
Go
1
star
48

watch-dragonball-xbmc

An addon that makes it possible to watch all Dragonball series through XBMC
Python
1
star
49

terraform-provider-ejson

A Terraform provider that can decrypt ejson on-the-fly
Go
1
star
50

exhaustive-enum

Exhaustive-enum is an exhaustive enum checker for Go. It can be used to make sure you don't miss any cases when switching over an enumeration.
Go
1
star
51

varlink_systemd

1
star