• Stars
    star
    3,649
  • Rank 11,570 (Top 0.3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 10 years ago
  • Updated 12 months ago

Reviews

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

Repository Details

Embed files into a Go executable

statik

Build Status

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

The command below will filter only files on listed extensions.

$ statik -include=*.jpg,*.txt,*.html,*.css,*.js

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)
  }
  
  // Serve the contents over HTTP.
  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.

You can also read the content of a single file:

import (
  "github.com/rakyll/statik/fs"

  _ "./statik" // TODO: Replace with the absolute import path
)

  // ...

  statikFS, err := fs.New()
  if err != nil {
    log.Fatal(err)
  }
  
  // Access individual files by their paths.
  r, err := statikFS.Open("/hello.txt")
  if err != nil {
    log.Fatal(err)
  }    
  defer r.Close()
  contents, err := ioutil.ReadAll(r)
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println(string(contents))

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.

Deterministic output

By default, statik includes the "last modified" (mtime) time on files that it packs. This allows an HTTP FileServer to present the correct file modification times to clients.

However, if you have a continuous integration task that checks that your checked-in static files in a git repository match the code that is generated on your CI system, you'll run into a problem: The mtime on the git checkout does not match what you have locally, causing tests to fail.

You can fix the test in one of two ways:

  1. In CI, manually set the mtime on the freshly checked out tree: here's a stackoverflow answer that provides a shell command to do that; or,
  2. Instruct statik not to store the "last modified" time.

To ignore the last modified time, use the -m to statik, like so:

$ statik -m -include=*.jpg,*.txt,*.html,*.css,*.js

Note that this will cause http.FileServer to consider the file to always have changed & serve it with a "Last-Modified" of the time of the request.

More Repositories

1

hey

HTTP load generator, ApacheBench (ab) replacement
Go
17,071
star
2

boom

HTTP(S) load generator, ApacheBench (ab) replacement, written in Go
Go
4,745
star
3

go-hardware

A directory of hardware related libs, tools, and tutorials for Go
Go
1,329
star
4

gom

A visual interface to work with runtime profiling data for Go
Go
1,295
star
5

gotest

go test with colors
Go
1,242
star
6

coop

Cheat sheet for some of the common concurrent flows in Go
Go
1,214
star
7

autopprof

Pprof made easy at development time for Go
Go
738
star
8

drive

Pull or push Google Drive files
Go
465
star
9

openai-go

Go client libraries for OpenAI
Go
443
star
10

go-test-trace

Go test with tracing.
Go
381
star
11

govalidate

Validates your Go installation and dependencies.
Go
335
star
12

portmidi

Go bindings for libportmidi
Go
282
star
13

globalconf

Persist flag values into an ini file
Go
275
star
14

magicmime

Go bindings for libmagic to detect MIME types
Go
216
star
15

ticktock

A cron job scheduler for Go
Go
209
star
16

go2xcode

Go package to Xcode project generator
Go
177
star
17

fake-it-til-you-make-it

A program that demonstrates that GitHub contribution graph can be cheated
Go
147
star
18

golambda

AWS Lambda Go functions made easy...
Go
137
star
19

launchpad

Talk to your Novation Launchpad in Go
Go
130
star
20

pprof-merge

Merge multiple pprof profile files into a single file
Go
118
star
21

events2prom

A metric collection daemon allows you to configure aggregations in runtime
Go
107
star
22

goproxy-s3

Go proxy that serves from S3
Go
104
star
23

go-sql-driver-spanner

Google Cloud Spanner driver for Go
Go
89
star
24

awesome-spanner

A curated list of awesome Google Cloud Spanner references, tools, libraries and more.
82
star
25

coinsensus

Decentralized distributed systems consensus
62
star
26

opencensus-grpc-demo

Export metrics and traces from gRPC servers and clients
Java
59
star
27

spannerz

Google Cloud Spanner Query Planner Visualizer
Go
59
star
28

aws-containers

My personal mind map of container platforms and tools on AWS
53
star
29

gowiki

Personal fork of Go wiki
47
star
30

command

CLI subcommands for Go
Go
44
star
31

trace

Drafts of a Dapper-style tracing client for Go
Go
44
star
32

obs-luts

LUT files to use with OBS
43
star
33

littlebits

littleBits Go module -- an io.Reader and io.Writer to read from and write to a circuit
Go
40
star
34

dfanout

A simple HTTP/2 fanout server
Go
38
star
35

goutil

Various Go utility packages.
Go
37
star
36

gce-metadata

GCE metadata command line tool
Go
32
star
37

myko

A simple attribution engine.
Go
28
star
38

log-to-xray

Write log entries, get X-Ray traces.
Go
27
star
39

pprof-upload

Upload pprof output to continuous profiler
Go
26
star
40

keys

Let your golang programs store and access passwords in a secure way
Go
26
star
41

go-numa

NUMA bindings for Go, requires libnuma.
Go
24
star
42

httpaa

Where http.HaandleFunc and alike lives.
Go
23
star
43

drummachine

Akai MPD18 replica written in Go, runs on mobile devices
Go
21
star
44

go-xcode

Reference Xcode project to build iOS apps with Go
Makefile
18
star
45

firmata

Firmata client for Go
Arduino
18
star
46

k8s-helloworld-leaderelection

Go
16
star
47

audio

Go
15
star
48

bubblr

Android client for App Engine Channels API
Java
15
star
49

rrqueue

Simple priority queues and round robin consumer for golang
Go
15
star
50

cosmicbackgroundmusic

Go
13
star
51

mysql-args-anonymizer

Go
13
star
52

sensors

Experiments with Android NDK sensors and Go
Go
13
star
53

redfail

Simple command to colorize the stderr of a target program
Go
12
star
54

opentelemetry-metric-go

Experiments, no real code.
Go
12
star
55

node-plussignin

Google+ Sign In middleware for Connect.
JavaScript
12
star
56

gcp-connectivitytests

Google Cloud Connectivity Tests
Go
11
star
57

perf2cloudprofiler

Upload perf output to Google Cloud profiler from anywhere
Go
10
star
58

drivefuse

Google Drive for Linux, BSD and MacOSX
C
10
star
59

grpcutil

Go
9
star
60

spanner-dotfiles

A set of personal aliases to use with Google Cloud Spanner.
7
star
61

talks

7
star
62

go-brillo

Go development notes for the Brillo targets. (External contributors who are willing to contribute are welcomed to comment.)
7
star
63

go-benchmarks

6
star
64

al

OpenAL bindings for Go (work-in-progress)
6
star
65

janis

The missing components of Android SDK
Java
6
star
66

pizza

rakyll.pizza
Go
6
star
67

rakyll.github.io

HTML
5
star
68

blinker

A tiny Raspberry Pi program controlled by a Go mobile app
Go
5
star
69

pubsub

Go
5
star
70

rakyll

Public profile...
5
star
71

refutil

Personal collection of reflection utils for Go
Go
4
star
72

benchmarks

Interesting Go benchmarks to watch
Go
4
star
73

experimental

Experimental Go packages
Go
3
star
74

node-ini

Simple ini reader for Node.js
JavaScript
3
star
75

snippetr

Extract code snippets from source code.
JavaScript
3
star
76

go0

$ docker run -i -t rakyll/go0
Dockerfile
2
star
77

ecs-metadata-proxy

Proxy to debug metadata server in ECS tasks
Go
2
star
78

aestaticdeploy

Takes a static directory and serves it on Google App Engine
Go
2
star
79

zigot

En extensible protocol for cloud storage, against of proprietary APIs.
2
star
80

appdatapreferences-localstorage

Syncs your local storage to Google Drive's appdata folder.
JavaScript
2
star
81

skeleton

skeletons for go projects
Go
1
star
82

travis-build

Go
1
star
83

trace2

Go
1
star
84

allgo

All Go is a repo to be used to test godoc command and godoc.org
Go
1
star
85

dockerfiles

All images are ARMv7
1
star
86

reviews

Catch all repo for reviewing things.
1
star