Jay Taylor's notes
back to listing indexrakyll/statik
[web search]Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
example | Fix examples | 10 months ago | |
fs | improve the performance of fs.Open() by unzipping in fs.New() and sim… | a year ago | |
.travis.yml | travis: update go versions (#33) | 9 months ago | |
LICENSE | Add LICENSE. | 4 years ago | |
README.md | Update README.md (#44) | 26 days ago | |
statik.go | Write build constraint tags with -tags (#38) | 3 months ago |
README.md
statik
statik allows you to embed a directory of static files into your Go binary to be later served from an http.FileSystem.
Is this a crazy idea? No, not necessarily. If you're building a tool that has a Web component, you typically want to serve some images, CSS and JavaScript. You like the comfort of distributing a single binary, so you don't want to mess with deploying them elsewhere. If your static files are not large in size and will be browsed by a few people, statik is a solution you are looking for.
Usage
Install the command line tool first.
go get github.com/rakyll/statik
statik is a tiny program that reads a directory and generates a source file that contains its contents. The generated source file registers the directory contents to be used by statik file system.
The command below will walk on the public path and generate a package called statik
under the current working directory.
$ statik -src=/path/to/your/project/public
In your program, all your need to do is to import the generated package, initialize a new statik file system and serve.
import ( "github.com/rakyll/statik/fs" _ "./statik" // TODO: Replace with the absolute import path ) // ... statikFS, err := fs.New() if err != nil { log.Fatal(err) } http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(statikFS))) http.ListenAndServe(":8080", nil)
Visit http://localhost:8080/public/path/to/file to see your file.
There is also a working example under example directory, follow the instructions to build and run it.
Note: The idea and the implementation are hijacked from camlistore. I decided to decouple it from its codebase due to the fact I'm actively in need of a similar solution for many of my projects.