Skip to content

📁 Embeds static resources into go files for single binary compilation + works with http.FileSystem + symlinks

License

Notifications You must be signed in to change notification settings

go-playground/statics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Package statics

Project status Build Status Go Report Card GoDoc License

Package statics embeds static files into your go applications. It provides helper methods and objects to retrieve embeded files and serve via http.

It has the following unique features:

  • Follows Symlinks! uses the symlinked file/directory name and path but the contents and other fileinfo of the original file.
  • Embeds static generation command for using go:generate in each static file for easy generation in production mode.
  • Handles multiple static go files, even inside of the same package.
  • Handles development (aka local files) vs production (aka static files) across packages.

Installation

	go get -u github.com/go-playground/statics

Then import the statics package into your own code.

import "github.com/go-playground/statics"

Usage and documentation

Please see https://godoc.org/github.com/go-playground/statics/static for detailed usage docs.

NOTE: when specifying path or directory name in code always use "/", even for you windows users, the package handles any conversion to you local filesystem paths; Except for the AbsPkgPath variable in the config.

run statics -h to see the options/arguments

Examples:

Embedding in Source Control

statics -i=assets -o=assets.go -pkg=main -group=Assets -ignore=\\.gitignore -init=true

Output:

//go:generate statics -i=assets -o=assets.go -pkg=main -group=Assets -ignore=\.gitignore

package main

import "github.com/go-playground/statics/static"

// newStaticAssets initializes a new *static.Files instance for use
func newStaticAssets(config *static.Config) (*static.Files, error) {

	return static.New(config, &static.DirFile{})
}

when using arg init=true statics package generates a minimal configuration with no files embeded; you can then add it to source control, ignore the file locally using git update-index --assume-unchanged [filename(s)] and then when ready for generation just run go generate from the project root and the files will get embedded ready for compilation.

Be sure to check out this packages best buddy https://github.com/go-playground/generate to help get everything generated and ready for compilation.

Example Usage

// generated via command: 
// statics -i=assets -o=assets.go -pkg=main -group=Assets -ignore=//.gitignore

gopath := getGopath() // retrieved from environment variable
pkgPath := "/src/github.com/username/project"

// get absolute directory path of the -i arguments parent directory + any prefix 
// removed, used when UseStaticFiles=false this is so even when referencing this 
// package from another project and your PWD is not for this package anymore the 
// file paths will still work.
pkg := goapth + pkgPath

config := &static.Config{
	UseStaticFiles: true,
	AbsPkgPath:     pkg,
}

// NOTE: Assets in the function name below is the group in the generation command
assets, err := newStaticAssets(config)
if err != nil {
	log.Println(err)
}

// when using http
http.Handle("/assets", http.FileServer(assets.FS()))

// other methods for direct access
assets.GetHTTPFile
assets.ReadFile
assets.ReadDir
assets.ReadFiles

Package Versioning

I'm jumping on the vendoring bandwagon, you should vendor this package as I will not be creating different version with gopkg.in like allot of my other libraries.

Why? because my time is spread pretty thin maintaining all of the libraries I have + LIFE, it is so freeing not to worry about it and will help me keep pouring out bigger and better things for you the community.

License

Distributed under MIT License, please see license file in code for more details.

About

📁 Embeds static resources into go files for single binary compilation + works with http.FileSystem + symlinks

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages