• Stars
    star
    2,404
  • Rank 18,353 (Top 0.4 %)
  • Language
    Go
  • License
    BSD 2-Clause "Sim...
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

go.rice is a Go package that makes working with resources such as html,js,css,images,templates, etc very easy.

go.rice

Build Status Godoc

go.rice is a Go package that makes working with resources such as html,js,css,images and templates easy. During development go.rice will load required files directly from disk. Upon deployment it's easy to add all resource files to a executable using the rice tool, without changing the source code for your package. go.rice provides methods to add resources to a binary in different scenarios.

What does it do

The first thing go.rice does is finding the correct absolute path for your resource files. Say you are executing a binary in your home directory, but your html-files are in $GOPATH/src/yourApplication/html-files. go.rice will lookup the correct path for that directory (relative to the location of yourApplication). All you have to do is include the resources using rice.FindBox("html-files").

This works fine when the source is available to the machine executing the binary, which is the case when installing the executable with go get or go install. But it does not work when you wish to provide a single binary without source. This is where the rice tool comes in. It analyses source code and finds call's to rice.FindBox(..). Then it adds the required directories to the executable binary, There are two strategies to do this. You can 'embed' the assets by generating go source code and then compile them into the executable binary, or you can 'append' the assets to the executable binary after compiling. In both cases the rice.FindBox(..) call detects the embedded or appended resources and load those, instead of looking up files from disk.

Installation

Use go install to install the package the rice tool.

go install github.com/GeertJohan/go.rice@latest
go install github.com/GeertJohan/go.rice/rice@latest

Package usage

Import the package: import "github.com/GeertJohan/go.rice"

Serving a static content folder over HTTP with a rice Box:

http.Handle("/", http.FileServer(rice.MustFindBox("http-files").HTTPBox()))
http.ListenAndServe(":8080", nil)

Serve a static content folder over HTTP at a non-root location:

box := rice.MustFindBox("cssfiles")
cssFileServer := http.StripPrefix("/css/", http.FileServer(box.HTTPBox()))
http.Handle("/css/", cssFileServer)
http.ListenAndServe(":8080", nil)

Note the trailing slash in /css/ in both the call to http.StripPrefix and http.Handle.

Loading a template:

// find a rice.Box
templateBox, err := rice.FindBox("example-templates")
if err != nil {
	log.Fatal(err)
}
// get file contents as string
templateString, err := templateBox.String("message.tmpl")
if err != nil {
	log.Fatal(err)
}
// parse and execute the template
tmplMessage, err := template.New("message").Parse(templateString)
if err != nil {
	log.Fatal(err)
}
tmplMessage.Execute(os.Stdout, map[string]string{"Message": "Hello, world!"})

Never call FindBox() or MustFindBox() from an init() function, as there is no guarantee the boxes are loaded at that time.

Calling FindBox and MustFindBox

Always call FindBox() or MustFindBox() with string literals e.g. FindBox("example"). Do not use string constants or variables. This will prevent the rice tool to fail with error Error: found call to rice.FindBox, but argument must be a string literal..

Tool usage

The rice tool lets you add the resources to a binary executable so the files are not loaded from the filesystem anymore. This creates a 'standalone' executable. There are multiple strategies to add the resources and assets to a binary, each has pro's and con's but all will work without requiring changes to the way you load the resources.

rice embed-go: Embed resources by generating Go source code

Execute this method before building. It generates a single Go source file called rice-box.go for each package. The generated go file contains all assets. The Go tool compiles this into the binary.

The downside with this option is that the generated go source file can become large, which may slow down compilation and requires more memory to compile.

Execute the following commands:

rice embed-go
go build

A Note on Symbolic Links: embed-go uses the os.Walk function from the standard library. The os.Walk function does not follow symbolic links. When creating a box, be aware that any symbolic links inside your box's directory are not followed. When the box itself is a symbolic link, the rice tool resolves its actual location before adding the contents.

rice append: Append resources to executable as zip file

This method changes an already built executable. It appends the resources as zip file to the binary. It makes compilation a lot faster. Using the append method works great for adding large assets to an executable binary.

A downside for appending is that it does not provide a working Seek method.

Run the following commands to create a standalone executable.

go build -o example
rice append --exec example

Help information

Run rice --help for information about all flags and subcommands.

You can use the --help flag on each sub-command. For example: rice append --help.

Order of precedence

When opening a new box, the rice.FindBox(..) tries to locate the resources in the following order:

  • embedded (generated as rice-box.go)
  • appended (appended to the binary executable after compiling)
  • 'live' from filesystem

License

This project is licensed under a Simplified BSD license. Please read the LICENSE file.

Package documentation

You will find package documentation at godoc.org/github.com/GeertJohan/go.rice.

More Repositories

1

gomatrix

Displays "The Matrix" in a terminal
Go
274
star
2

openwrt-go

Fork of the openwrt project to add gccgo and libgo
C
244
star
3

yubigo

Yubigo is a Yubikey client API library that provides an easy way to integrate the Yubico Yubikey into your existing Go-based user authentication infrastructure.
Go
124
star
4

go.tesseract

go.tesseract is a wrapper for the tesseract-ocr library.
Go
68
star
5

go.linenoise

Linenoise is a lightweight readline library.
C
57
star
6

go.hid

Provides communication with USB Human Interface Devices.
C
53
star
7

go.qrt

QR codes for terminals
Go
24
star
8

fgt

fgt runs any command for you and exits with exitcode 1 when the child process sent anything to stdout or stderr
Go
24
star
9

ango

ango is a tool that generates a protocol for communication between Go and AngularJS over http/websockets.
Go
15
star
10

cgo.wchar

The cgo.wchar package is to be used with go/cgo and helps with the conversion from and to C.wchar_t and wchar_t strings (*C.wchar_t with null terminator or length int).
Go
14
star
11

go.hue

go.hue is a Go package wrapping the Philips Hue bridge interface
Go
14
star
12

go.bitcoin

Go package containing bitcoin utilities and BitcoindClient wrapping the http json-api.
Go
13
star
13

go.leptonica

go.leptonica wraps the leptonica library for "efficient image processing and image analysis operations".
Go
12
star
14

go.ask

Ask questions in cli
Go
8
star
15

aerospike-capacity-calculator

Aerospike capacity calculator
HTML
8
star
16

go.geofence

Go
6
star
17

aerospike-discovery

Aerospike cluster node discovery with etcd
Go
6
star
18

go.incremental

package incremental provides concurency-safe incremental numbers
Go
5
star
19

tune

Tune is a music-player for the AudioAddict web radio's such as Digitally Imported (di.fm) and RadioTunes.
Go
4
star
20

dockerfiles

Dockerfiles used to build my public images at
4
star
21

kube-multi-config

4
star
22

go.cef

CEF for Go. Work in progress..
Go
3
star
23

gocaptcha

Go
2
star
24

gopaint

Go Paint
Go
2
star
25

go.airbrake

Airbrake package
Go
2
star
26

reporef

Go
2
star
27

diffsim

Cryptocoin difficulty re-adjustment algorithm simulator
Go
2
star
28

go-sourcepath

Package sourcepath gives the absolute path for the calling Go file. Extracted from go.rice
Go
1
star
29

go.bapp

Simple library to create a go application with a webinterface as gui (browser, localhost)
Go
1
star
30

ango-old

AngularJS & Go. Communication and data-binding
Go
1
star
31

docker-xpra

XPRA on Docker
1
star
32

mgl

MSI GT laptop keyboard LEDs controller
Go
1
star
33

gotalks

This repo holds slides and stuff for talks
Go
1
star
34

outyet

Server program powering outyet.org
Go
1
star
35

unsolicited-advice

Rust
1
star
36

go.btcqr

bitcoin QR code generator
Go
1
star