assets

package module
v0.0.0-...-4f4301a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2016 License: BSD-3-Clause Imports: 11 Imported by: 154

README

go-assets

go-assets is a simple embedding asset generator and consumer library for go. The main use of the library is to generate and embed small in-memory file systems ready to be integrated in webservers or other services which have a small amount of assets used at runtime. This is great for being able to do single binary deployments with assets.

The Generator type can be used to generate a go file containing an in-memory file tree from files and directories on disk. The file data can be optionally compressed using gzip to reduce file size. Afterwards, the generated file can be included into your application and the assets can be directly accessed without having to load them from disk. The generated assets variable is of type FileSystem and implements the os.FileInfo and http.FileSystem interfaces so that they can be directly used with http.FileHandler.

See also go-assets-builder for a simple builder program using go-assets and exposing the generator as a command line application.

See http://godoc.org/github.com/jessevdk/go-assets for more information.

Documentation

Overview

go-assets is a simple embedding asset generator and consumer library for go. The main use of the library is to generate and embed small in-memory file systems ready to be integrated in webservers or other services which have a small amount of assets used at runtime. This is great for being able to do single binary deployments with assets.

The Generator type can be used to generate a go file containing an in-memory file tree from files and directories on disk. The file data can be optionally compressed using gzip to reduce file size. Afterwards, the generated file can be included into your application and the assets can be directly accessed without having to load them from disk. The generated assets variable is of type FileSystem and implements the os.FileInfo and http.FileSystem interfaces so that they can be directly used with http.FileHandler.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// The full asset file path
	Path string

	// The asset file mode
	FileMode os.FileMode

	// The asset modification time
	Mtime time.Time

	// The asset data. Note that this data might be in gzip compressed form.
	Data []byte
	// contains filtered or unexported fields
}

An asset file.

func (*File) Close

func (f *File) Close() error

func (*File) IsDir

func (f *File) IsDir() bool

func (*File) ModTime

func (f *File) ModTime() time.Time

func (*File) Mode

func (f *File) Mode() os.FileMode

func (*File) Name

func (f *File) Name() string

func (*File) Read

func (f *File) Read(data []byte) (int, error)

func (*File) Readdir

func (f *File) Readdir(count int) ([]os.FileInfo, error)

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

func (*File) Size

func (f *File) Size() int64

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

func (*File) Sys

func (f *File) Sys() interface{}

type FileSystem

type FileSystem struct {
	// A map of directory paths to the files in those directories.
	Dirs map[string][]string

	// A map of file/directory paths to assets.File types.
	Files map[string]*File

	// Override loading assets from local path. Useful for development.
	LocalPath string
}

An in-memory asset file system. The file system implements the http.FileSystem interface.

func NewFileSystem

func NewFileSystem(dirs map[string][]string, files map[string]*File, localPath string) *FileSystem

func (*FileSystem) NewFile

func (f *FileSystem) NewFile(path string, filemode os.FileMode, mtime time.Time, data []byte) *File

func (*FileSystem) Open

func (f *FileSystem) Open(p string) (http.File, error)

Implementation of http.FileSystem

type Generator

type Generator struct {
	// The package name to generate assets in,
	PackageName string

	// The variable name containing the asset filesystem (defaults to Assets),
	VariableName string

	// Strip the specified prefix from all paths,
	StripPrefix string
	// contains filtered or unexported fields
}

An asset generator. The generator can be used to generate an asset go file with all the assets that were added to the generator embedded into it. The generated assets are made available by the specified go variable VariableName which is of type assets.FileSystem.

Example
g := Generator{}

if err := g.Add("."); err != nil {
	panic(err)
}

// This will write a go file to standard out. The generated go file
// will reside in the g.PackageName package and will contain a
// single variable g.VariableName of type assets.FileSystem containing
// the whole file system.
g.Write(os.Stdout)
Output:

func (*Generator) Add

func (x *Generator) Add(p string) error

Add a file or directory asset to the generator. Added directories will be recursed automatically.

func (*Generator) Write

func (x *Generator) Write(wr io.Writer) error

Write the asset tree specified in the generator to the given writer. The written asset tree is a valid, standalone go file with the assets embedded into it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL