- Notifications
You must be signed in to change notification settings - Fork0
inabyte/embed
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Takes a list for file or folders (likely atgo generate
time) andgenerates Go code that statically implements the a http.FileSystem.
Features:
embed
:
- outputs
gofmt
ed andlint
ed Go code. - produces go-gettable go and go assembly sources with
go generate
. - keeps data and strings in read-only section of the binary.
- minify HTML, CSS, and JavaScript files.
- compress compressible files with
gzip
. - provideshttp.FileSystem API.
- provideshttp.Handler handler(if requested),
- generates a test code.
- allows access to compressed data.
- serving gzip files to clients that accept compressed content.
- calculate checksums for serving Etag-based conditional requests.
- zero dependencies on packages outside the standard library(if requested),
The minifier usedisgithub.com/tdewolff/minify.
The source paramaters are a list of folder and/or files, with an optional marker for prefix start.The prefix marker is<->
, if you specify a path of./files/html<->/embed
it will importeverything under the folder./files/html/embed
as/embed/...
for example./files/html/embed/index.html
will be stored as/embed/index.html
.There is no limit to the number you can specify but the resultant file system must be unique,if the processing produces two files in the same locationembed
will error out.
First, get the command-line tool:
go get github.com/inabyte/embed/cmd/embed
embed [flags] [files...]
Flags Are:
-o="embed" Output files base name.-pkg="" Package name defauls to directory of output.-tags="" Build tags added to output files.-ignore="" Regexp for files we should ignore (for example \\\\.DS_Store).-include="" Regexp for files to include. Only files that match will be included.-minify="application/javascript,text/javascript,text/css,text/html,text/html; charset=utf-8" Comma list of mimetypes to minify.-modifytime="" Unix timestamp to override as modification time for all files.-no-compress If set, do not compress files.-go If set, write only go files-fileserver If set, produce http server code-binary If set, produce self-contained extractor/http server binary (-o will become the binary name)-noremote If set, force zero dependencies on packages outside the standard library.-nolocalfs If set, do not store local file system paths.
Embedded assets can be served with HTTP using thehttp.Server
.Assuming a directory structure similar to below:
.├── main.go└── html ├── css │ └── style.css ├── scripts │ └── utils.js └── index.html
Where main.go contains:
package mainimport ( "log" "net/http")func main() { // FileHandler() is created by embed and returns a http.Handler. log.Fatal(http.ListenAndServe(":8080", FileHandler()))}
- Generate the embedded data:
embed -o static -pkg=main html
- Start the server:
go run .
- Accesshttp://localhost:8080/ to view the content.
You can see a worked example inexamples/binary dirjust run it asgo run ./examples/binary
// Config contains all information needed to run embed.type Config struct {// Output is the file to write output.Output string// Package name for the generated file.Package string// Ignore is the regexp for files we should ignore (for example `\.DS_Store`).Ignore string// Include is the regexp for files to include. If provided, only files that// match will be included.Include string// Minify is comma separated list of mime type to minify.Minify string// ModifyTime is the Unix timestamp to override as modification time for all files.ModifyTime string// DisableCompression, if true, does not compress files.DisableCompression bool// Binary, if true, produce self-contained extractor/http server binary.Binary bool// NoRemote, if true, zero dependencies on packages outside the standard library.NoRemote bool// Go, if true, creates only go files.Go bool// NoLocalFS, if true, do not store local file system paths.NoLocalFS bool// FileServer, if true, add http.Handler to serve files.FileServer bool// BuildTags, if set, adds a build tags entry to file.BuildTags string// Files is the list of files or directories to embed.Files []string}
Embedded assets can be served with HTTP using thehttp.Server
.Assuming a directory structure similar to below:
.├── main.go└── html ├── css │ └── style.css ├── scripts │ └── utils.js └── index.html
Where main.go contains:
package mainimport ( "log" "net/http")func main() { // FileHandler() is created by embed and returns a http.Handler. log.Fatal(http.ListenAndServe(":8080", FileHandler()))}
And embed_static.go contains:
// +build ignorepackage mainimport ( "github.com/inabyte/embed")func main() { config := embed.New() config.Output = "static" config.Package = "main" config.Files = []string{"html"} config.FileServer = true config.NoLocalFS = true config.Generate()}
- Generate the embedded data:
go run embed_static.go
- Start the server:
go run .
- Accesshttp://localhost:8080/ to view the content.
You can see a worked example inexamples/code dirjust run it asgo run ./examples/code
About
Embed static file in go