• This repository has been archived on 04/Mar/2021
  • Stars
    star
    109
  • Rank 319,018 (Top 7 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

[OBSOLETE] Go asset embedding for use with `go generate`

Becky -- Go asset embedding for use with go generate

Becky embeds assets as string literals in Go source.

OBSOLETE Becky has been made unnecessary by the embed package in Go 1.16. It will not be developed further.

Usage

Use becky via tools.go, see https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module

For example, in your project create tools.go that has:

// +build tools

package tools

import (
	_ "github.com/tv42/becky"
)

And then near where you use the asset, put

//go:generate go run github.com/tv42/becky index.html

and run

$ go generate

This will create new files, named *.gen.go. You should add those into your version control system, to ensure go get works for others.

You can pass multiple asset files at once, or repeat the go:generate line.

Variable name

The generated files declare variables that now contain your assets. Given the above index.html, the variable will be named index.

You can override the name with -var=NAME, or skip it with -var=_ and use side effects in your wrapper function (discussed later).

The asset will be an value of type asset (this code is autogenerated, you don't need to type it in):

type asset struct {
	Name    string
	Content string
	...
}

Name has the original filename, as a hint for Content-Type selection.

Wrapper

For most uses, an asset value needs to be given application or file type specific functionality. To make this easy, the asset value will be passed to a function caller wrapper. You need to write these wrapper functions.

The name of the default wrapper is the (final) extension of the asset filename. For index.html, that's html. You can override the wrapper with -wrap=NAME.

In your application, you'd do something like

func html(a asset) http.Handler {
	return a
}

or

func txt(a asset) string {
	return a.Content
}

or

func tmpl(a asset) *template.Template {
	return template.Must(template.New(a.Name).Parse(a.Content))
}

to smartly handle *.html, *.txt and *.tmpl assets. Feel free to pass the fields of asset to a factory function or type that matches what you need, or use the asset, whatever suits your project.

HTTP

Type asset implements http.Handler, including ETag cache validation. It uses http.ServeContent which will set Content-Type from the file name or content, and handle Range requests.

Code generation speed

If repeated calls to go run github.com/tv42/becky are too slow, you can build it once and run from there:

//go:generate go build github.com/tv42/becky
//go:generate ./becky index.html

Build speed

gc, the Go compiler, can slow down with large source files. As e.g. image assets can get big, this can start to slow down your builds. The mechanism used for embedding has been chosen to be the most efficient available.

Embedding a 10MB asset (creating a 28MB Go source file) takes <1 second to generate the code and about 1 second for every compilation.

You can minimize the number of times assets need to be compiled by putting them in a different package that updates less often than most of your source.

Development mode

If you build your application with -tags dev, asset.ServeHTTP will reload the asset from disk on every request, and not use the embedded copy. This makes editing HTML, CSS and such more convenient.

More Repositories

1

gitosis

software for hosting git repositories
Python
412
star
2

alone

Go Alone is an experiment into running Go as an appliance-oriented operating system
Go
77
star
3

httpunix

Go library to talk HTTP over Unix domain sockets
Go
47
star
4

zbase32

Human-oriented encoding for binary data
Go
41
star
5

benchmark-ordered-map

Ordered map Go data structure benchmarks
Go
40
star
6

base58

Base58 encoding for Go
Go
38
star
7

jsonarray

Streaming decoder for JSON arrays
Go
37
star
8

bella

Bella renders text to graphics and prints it on a label maker using IPP/CUPS.
Go
37
star
9

birpc

Bi-directional RPC library for Go, including JSON-over-WebSocket
Go
31
star
10

topic

Go library for in-process single-topic pub-sub
Go
30
star
11

troops

A software deployment tool (THIS PROJECT IS IN DEEP HIBERNATION FOR NOW. MOVE ALONG.)
Python
29
star
12

yubage

`age-plugin-yubikey` implementation, encrypt things with a Yubikey/any PIV card
Go
25
star
13

mockhttp

Mock object for Go http.ResponseWriter
Go
23
star
14

securityblanket

DIY home security project using Honeywell 5800 series RF sensors
Go
13
star
15

sparkbar

Draw a sparkline in a terminal with UTF-8 block characters
Go
13
star
16

demand

Download, build, cache and run a Go app easily.
Go
12
star
17

adhoc-httpd

Quick & dirty HTTP static file server
Go
11
star
18

slug

Create slugs of text or URLs, for use in e.g. URL path segments
Go
10
star
19

jog

Structured logging library for Go
Go
10
star
20

humanize-bytes

Command-line utilities to convert "MiB" etc to raw numbers, and back
Go
10
star
21

msgpack-json

Command-line utilities to convert between msgpack and json
Go
7
star
22

ldaptor

LDAP server, client and utilities, using Twisted Python. Historical interest only, see link for ongoing community project.
Python
7
star
23

representative

Static slideshow generator for Go slides
JavaScript
6
star
24

quobar

X11 status bar
Go
6
star
25

oppositus

mirror CoreOS releases
Go
6
star
26

sinus

Command-line remote control for Sonos/UPnP audio devices
Go
6
star
27

x11-clipboard

Interacting with the X11 clipboard/selection from Go
Go
5
star
28

darwini

Go web programming with strong types & strong opinions
Go
5
star
29

spindown-idle-disks

Spin down idle SATA disks. Because hdparm -S 120 just won't work.
Go
5
star
30

staged

Run a command with the Git staged files
Go
5
star
31

compound

Go library for making order-preserving keys from structured data, with support for prefix iteration
Go
5
star
32

audibly

Audibly report command status
Go
5
star
33

listen-like-systemd

Command to emulate systemd socket activation fd passing
Go
4
star
34

toursst

TouRSSt fetches RSS feeds into Maildirs. Mostly of historical interest.
Python
4
star
35

downburst

Fast Ubuntu Cloud Image creation on libvirt
Python
3
star
36

cheesy2

Provide Amazon EC2-style user data to libvirt virtual machine -- Abandoned in favor of Downburst, https://github.com/ceph/downburst
Python
3
star
37

where

Find where a Go identifier is defined
Go
3
star
38

ntlmv2hash

Compute Windows NTLMv2 password hashes
Go
3
star
39

bugit

A prototype of bug tracking with Git
Python
3
star
40

mobiledoc-to-markdown

Convert Mobiledoc (Ghost blog) articles to Markdown
Go
3
star
41

cliutil

Go library to make subcommand-style command line interfaces easier to program
Go
3
star
42

twackup

Backs up your tweets, a flimsy excuse to write more Go code
Go
2
star
43

seed

Go library to easily seed PRNGs with some entropy
Go
2
star
44

varint

Variable integer encoding/decoding tools
Go
2
star
45

read-file-to-env

Read files into environment variables and execute command
Go
2
star
46

eunuchs

Missing manly parts of UNIX API for Python
C
2
star
47

oatmail

synchronizing Maildir mail with git
Python
2
star
48

json-point

Command-line tool to extract parts of JSON documents, with JSON Pointer syntax
Go
2
star
49

s3-url-sign

Sign a URL for use with S3
Python
2
star
50

scalemail

Scalable virtual mail domain system built on Postfix and LDAP. Mostly of historical interest.
Python
2
star
51

wait-for-pid

Wait for processes to exit
Rust
2
star
52

scram-password

Command-line utility for Postgres-compatible SCRAM-SHA-256 passwords
Go
2
star
53

big

[DEPRECATED] Large file storage with Git; see instead https://github.com/bazil/plop
Python
1
star
54

didyouseethis

Retweet anything with certain keywords
Go
1
star
55

dia_for_sphinx

Dia directive plugin for Sphinx
Python
1
star
56

childrpc

Use Go's rpc package to talk to subprocesses
Go
1
star
57

sekrit

[DEPRECATED] manage multi-user GPG-protected secrets; see instead https://github.com/tv42/yubage
Python
1
star
58

phrase-encoder

Encode binary data as words
Go
1
star
59

old-go

Git mirror of the Go Mercurial repository, with my modifications
Go
1
star
60

fs

Common Pythonic Filesystem API. Historical interest only.
Python
1
star
61

choosy

experimental & minimal web-based user interface to select a file from a list and play with mpv
Rust
1
star
62

dnscvsutil

Maintain DNS zone files under CVS control (ancient, historical interest mostly)
1
star
63

bearproxy

A very simple HTTP reverse proxy that checks that requests contain a valid secret as a bearer token
Go
1
star
64

intrusive

Intrusive container data types for Go
Go
1
star
65

snakepit

HiveDB partitioned database schema Python client. Abandoned in favor of using Cassandra. Mostly of historical interest.
Python
1
star
66

maildir-fix

Fix maildirs after git has pruned empty dirs
Go
1
star
67

botosh

Start a Python shell with Boto connections open
Python
1
star
68

x11-media-keys

Adjust volume and screen brightness based on keypresses
Go
1
star
69

melange-discovery

Draft blueprint for Openstack Melange instance address discovery component
1
star