Code Monkey home page Code Monkey logo

embed's Introduction

embed

Static content embedding for Golang, ©2017 Adrian Price. Usage indicates acceptance of the license found in the LICENSE file.

Purpose & Functionality

Embed is a tool for embedding static content files into a Go binary, to create a single, self-contained binary. Embed uses build constraints to offer a development mode that loads content from disk for rapid iteration during local development; once content is stable, you can run go generate to update the embedded content files, and then rebuild with different tags to serve the embedded content instead of the files on disk.

In addition to simply embedding content within your binary, embed will minify HTML, CSS, and JavaScript; gzip files for serving to clients that accept compressed content; and calculate checksums for handling Etag-based conditional requests.

Installation

Embedding tool: go get github.com/aprice/embed/cmd/embed

Usage library: go get github.com/aprice/embed/loader

Usage

Embed can easily be run with go generate.

//go:generate embed -c "embed.json"

To use embed, create a config file specifying what's to be generated:

{
	"RootPath": ".",
	"Recurse": true,
	"IncludePattern": "",
	"ExcludePattern": "(^\\.|\\.go$)",
	"OutputPath": "embedded.go",
	"BuildConstraints": "",
	"PackageName": "embedded",
	"DevOutputPath": "",
	"DevBuildConstraints": "",
	"MinifyTypes": {
		"\\.html?$": "text/html",
		"\\.css$": "text/css",
		"\\.js$": "application/javascript"
	},
	"CompressPattern": "\\.(css|js|html)$",
	"NoCompressPattern": "\\.(jpe?g|png|gif|woff2?|eot|ttf|ico)$",
	"OverrideModDate": false
}

The values above are the defaults.

RootPath is the directory where source files to be embedded will be scanned. If Recurse is true, subdirectories will be scanned as well. Each entry will be compared against IncludePattern and ExcludePattern; if a file does not match IncludePattern or does match ExcludePattern, it will not be included.

OutputPath is the path where the embedded content go file will be written. If BuildConstraints is not empty, it will be added to the output file; for example, "BuildConstraints": "!dev" will result in a file that will not be built by a build command including -tags="dev". PackageName is the package name that will be used for the output file.

DevOutputPath and DevBuildConstraints work the same as their non-Dev counterparts, but apply to a separate "dev mode" file; if DevOutputPath is not empty, a dev mode file will be written which reads all content from disk instead of using embedded content. This allows for rapid iteration during local development. The dev mode file will use PackageName for its package.

MinifyTypes is a mapping of file name regular expressions to content types that should be minified. Minifiers are enabled for text/html, text/css, text/javascript (or application/javascript), and image/svg+xml. Any other content type (or an empty content type) will not be minified. The minifier used is github.com/tdewolff/minify.

CompressPattern is a regular expression matching file names that should be gzip compressed for clients that accept compressed data. NoCompressPattern is for excluding files which otherwise match CompressPattern.

If OverrideModDate is true, the modification date for embedded files will be set to the timestamp when generation is run; otherwise, it will be the modification date of the source files.

Referencing Embedded Content

To reference embedded content, call the GetEmbeddedContent() function in the package where your generated file was created. This returns a Loader, which can be used directly as an http.Handler to serve the embedded content the same as http.FileServer(http.Dir(RootPath)). It also exposes a GetContents() method, for loading embedded content as a byte slice for programmatic use, such as embedding template files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.