• Stars
    star
    300
  • Rank 133,683 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 6 years ago
  • Updated over 5 years ago

Reviews

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

Repository Details

A simple tool for embedding assets in a Go binary.

genesis Go Report Card

Genesis is a utility for embedding static file assets into Go files to be included in static compilation. Genesis includes support for http.FileSystem as well versioning via SHA256 hashes.

Installation

To install genesis, first install Go and then run:

$ go get -u github.com/benbjohnson/genesis/...

You should now have a genesis binary in your $GOPATH/bin directory.

Usage

Command line usage

The genesis command includes a few basic options available from the CLI:

usage: genesis [options] path [paths]

Embeds listed assets in a Go file as hex-encoded strings.

The following flags are available:

	-pkg name
		Package name of the generated Go file. Required.

	-o output
		Output filename for generated code. Optional.
		(default stdout)

	-C dir
		Execute genesis from dir. Optional.

	-tags tags
		Comma-delimited list of build tags. Optional.

You can generate a new Go file with your embedded assets by specifying the package name and the list of files or directories you want to include. By default the output will go to STDOUT so you can redirect to the file of your choice.

$ genesis -pkg mypkg file1 file2 dir1 > assets.gen.go

Specifying the output file

You can also specify the output file using the -o flag. This is useful if you are running via //go:generate and file redirection is not available:

$ genesis -pkg mypkg -o assets.gen.go file1 file2 dir1

Working directory

One common approach to asset organization is to create a separate assets package that includes your asset files and your generated embedded file. You can use the -C flag to change the present working directory to this package and then execute relative to this directory:

$ genesis -C assets -pkg mypkg -o assets.gen.go file1 file2

Including build tags

Finally, you can optionally include build tags to optionally include assets in your final build. Tags should be comma separated for each individual build tag line:

$ genesis -pkg mypkg -o assets.386.gen.go -tags "linux darwin,386" file1 file2 dir1

This will output the following build tag comments at the top of the file:

// +build linux darwin
// +build 386

HTTP File System

Your generated embedded assets Go file will include an implementation of http.FileSystem that can be passed to http.FileServer to automatically serve your embedded assets through an http.Handler:

http.Handle("/", http.FileServer(assets.FileSystem()))

Cache control

Your embedded assets Go file also includes a method for returning the filename with an included SHA256 hash. This hash will change whenever the contents of the file changes so the http.FileSystem will tell the browser to cache the file indefinitely using the Cache-Control header.

To use this feature, simply use the AssetNameWithHash() function when writing your URLs that reference embedded assets:

fmt.Fprintf(w, `<script src="%s"></script>`, assets.AssetNameWithHash("bundle.js"))

This will output something like the following:

<script src="/bundle-c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2.js"></script>

When a file is requested with a SHA256 hash it will be cached indefinitely which leads to improved web site latency on return visits.

Programmatic API

The generated embedded assets Go file includes several types and utility functions. You can view this information via the godoc for your package. A list of some of these types and functions are included below.

File

One File is generated for every asset you embed. This struct provides the name, content hash, size, last modification time, and raw data.

type File struct {}

func (f *File) Name() string
func (f *File) Hash() string
func (f *File) ModTime() time.Time
func (f *File) Data() []byte

Asset functions

Several utility functions are provided for accessing asset information. The Asset() function simply returns the underlying asset data while AssetFile() returns the entire File object. To retrieve a sorted list of all asset names, use the AssetNames() function.

func Asset(name string) []byte
func AssetFile(name string) *File
func AssetNames() []string

Content hash functions

The AssetNameWithHash() function will return a given asset name with its SHA256 content hash included. This is useful for embedding in HTML source names when combining with the included http.FileSystem implementation.

func AssetNameWithHash(name string) string

There are also utility functions for working with asset names and hashes. The JoinNameHash() function will build an asset name hash with the given name and hash. The SplitNameHash() function will return the base asset name and an asset name hash. The HasNameHash() returns true if a given name includes an asset hash.

func JoinNameHash(name, hash string) string
func SplitNameHash(name string) string 
func HasNameHash(name string) bool

More Repositories

1

litestream

Streaming replication for SQLite.
Go
9,990
star
2

thesecretlivesofdata

Understanding what your bits do when you're not looking.
JavaScript
3,338
star
3

wtf

WTF Dial is an example web application written in Go.
Go
1,533
star
4

postlite

Postgres wire compatible SQLite proxy.
Go
1,202
star
5

immutable

Immutable collections for Go
Go
686
star
6

clock

Clock is a small library for mocking time in Go.
Go
665
star
7

ego

An ERB-style templating language for Go.
Go
578
star
8

testing

A small collection of functions for Go testing.
Go
525
star
9

megajson

A JSON parser generator for high performance encoding and decoding in Go.
Go
468
star
10

hashfs

Implementation of io/fs.FS that appends SHA256 hashes to filenames to allow for aggressive HTTP caching.
Go
342
star
11

sql-parser

Toy SQL parser example for Gopher Academy
Go
324
star
12

phantomjs

Go client for PhantomJS.
Go
294
star
13

jmphash

Implementation of the Jump Consistent Hash algorithm in Go.
Go
154
star
14

scuttlebutt

A daemon for tracking and tweeting trending Github repositories by language.
Go
150
star
15

llvm-c-kaleidoscope

An implementation of the Kaleidoscope language using Flex, Bison & the LLVM-C bindings.
C
129
star
16

playback.js

A library for dynamic timeline playback.
JavaScript
117
star
17

litestream-docker-example

An example of using Litestream within a Docker container.
Go
88
star
18

css

W3C-compliant CSS3 parser and scanner
Go
84
star
19

litestream-s6-example

Example repository for building a multi-process Docker container.
Dockerfile
83
star
20

tmpl

Command line interface to Go's text/template library.
Go
81
star
21

grapevine

Trending topics for stuff you care about
Ruby
80
star
22

slowweb

An HTTP request governor
Ruby
75
star
23

litestream-read-replica-demo

A demo application for running live read replication on fly.io with Litestream
Go
69
star
24

ghfs

FUSE Filesystem for the GitHub API
Go
61
star
25

agency

A fast user agent string parser for Go.
Go
60
star
26

litestream-library-example

Example repository for embedding Litestream in a Go application.
Go
52
star
27

litestream-read-replica-example

An example of using Litestream's live read replication feature.
Go
52
star
28

melomel

External ActionScript Interface.
ActionScript
42
star
29

litestream.io

SCSS
40
star
30

pprofdump

A simple utility for collecting net/http/pprof profiles.
Go
28
star
31

peapod

A personal podcast service.
Go
26
star
32

application-development-using-boltdb

Repository for my "Application Development Using BoltDB" talk
Go
26
star
33

sieve

A command line utility for graphing piped data.
Go
23
star
34

production-sqlite-go

Companion repository for GopherCon presentation on "Production Applications Using SQLite & Go"
Go
23
star
35

goo

Thin wrapper for the Go toolchain.
Go
20
star
36

burger-stack

Presentation for "The Burger Stack"
19
star
37

structuring-applications-for-growth

GopherCon 2016 presentation for "Structuring Applications for Growth"
17
star
38

stack

Go debug/stack utility functions.
Go
17
star
39

myapp

An simple application with an HTTP server & SQLite database.
Go
14
star
40

glee

Incomplete Go port of the KLEE SymEx system.
Go
14
star
41

melomel.rb

An external interface to Flash from Ruby.
Ruby
13
star
42

raft.js

An experimental Raft implementation in Javascript.
13
star
43

vex

Variable-length, lexicographically-sortable hex format for uint64 values.
Go
13
star
44

gha

SQLite load testing application using GitHub Archive data.
Go
13
star
45

writing-a-distributed-systems-library

Companion code for the Gopher Academy blog post.
Go
11
star
46

tiny-ego

A toy application showcasing ego templates & components.
Go
10
star
47

boxer

A little app that boxes my time.
Go
10
star
48

bootstrap-ego

An ego template component library for Bootstrap 4.
Go
10
star
49

seppuku

To be executed upon implementation of generics in Go.
Go
10
star
50

roommate

A conference room scheduling application.
Go
9
star
51

syncutil

A collection of utility functions for Go synchronization.
Go
9
star
52

gist

Gist hosting and embedding.
Go
8
star
53

melomel-examples

Examples of using Melomel.
Ruby
8
star
54

describe.today

A web site to describe today.
JavaScript
7
star
55

minipack

A lightweight C MessagePack parser.
C
7
star
56

skybox

An open source funnel analysis application.
Go
7
star
57

go-raft-runner

A test runner application for the go-raft library.
Go
6
star
58

http-wiretap

It's like Charles Proxy for your Rubies!
Ruby
5
star
59

mincore

Example usage for using mincore() in Go.
Go
5
star
60

miniviz

A simplified interface to GraphViz for laying out clusters, nodes and edges.
Ruby
5
star
61

graphviz-as3

An interface to the graphviz CLI using Adobe AIR.
ActionScript
5
star
62

edb

A simple database for tracking events.
Go
5
star
63

rationl

Online journal for tracking experiments.
Go
4
star
64

sqlite-bench

Miscellaneous Go/SQLite benchmarks
Go
4
star
65

hackerbeeper

A dumb utility for playing generated notes when you type.
Go
4
star
66

opus

Command line utility for printing columnized code.
4
star
67

mockdown.as

A Markdown-inspired Mockup Language
ActionScript
4
star
68

serialkiller

ActionScript JSON & XML serialization library
ActionScript
4
star
69

whodump

A command line interface for checking domain name availability.
Ruby
4
star
70

tsld.js

Core library for "The Secret Lives of Data" project.
JavaScript
3
star
71

bandicoot

A crash reporter library for C applications.
C
3
star
72

skydb.io

The Official Sky Web Site
CSS
2
star
73

cine

A movie search application.
Go
2
star
74

chatter

Demo application using Server Side Events (SSE) and written in Go.
Go
2
star
75

authoritarian

Command line utility for authorizing Twitter users to an application.
2
star
76

matlock

Simple name extraction utility.
Ruby
2
star
77

constdump

Utility for printing a list of package-level constants.
Go
2
star
78

d3.jquery.js

A collection of D3.js charts made available as jQuery plugins.
JavaScript
1
star
79

mockdown.rb

Mockups for hackers
Ruby
1
star
80

landmarkd

The Landmark Tracking Server.
Go
1
star
81

timeshifter

A Ruby library for shifting time.
Ruby
1
star
82

httpng

A local server for saving HTML elements as PNG files.
JavaScript
1
star
83

ldbchk

Runs concurrency tests against LevelDB & Levigo.
Go
1
star
84

whollydeliciousfoods.com

The home page for Wholly Delicious Foods.
JavaScript
1
star
85

homebrew-litestream

Homebrew tap for litestream.
Ruby
1
star
86

bench-c

A small collection of benchmarks for the C programming language.
C
1
star
87

gitcoin

Turing gitcoin repository
Go
1
star
88

unistat

A utility for calculating simple statistics on unicode characters.
Go
1
star
89

termgraf

Terminal chronograf
Go
1
star
90

locald

A simple HTTP server for serving static files out of the current directory.
1
star
91

skylandlabs.github.com

Skyland Labs Blog
JavaScript
1
star
92

fql-editor

An editor for FQL queries.
ActionScript
1
star
93

tip.litestream.io

Mirror of litestream.io repository for latest changes.
HTML
1
star
94

fslice

A small utility for extracting delimited sections of a file.
Go
1
star
95

ego-example

A simple ego templating example.
Go
1
star
96

prog

Go testing
Go
1
star