• Stars
    star
    68
  • Rank 457,543 (Top 10 %)
  • Language
    Go
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 8 years ago

Reviews

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

Repository Details

๐Ÿ“ Embeds static resources into go files for single binary compilation + works with http.FileSystem + symlinks

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.

More Repositories

1

validator

๐Ÿ’ฏGo Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
Go
16,425
star
2

webhooks

๐ŸŽฃ Webhook receiver for GitHub, Bitbucket, GitLab, Gogs
Go
949
star
3

form

๐Ÿš‚ Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.
Go
750
star
4

pool

๐Ÿšค a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation
Go
724
star
5

lars

๐Ÿšจ Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.
Go
386
star
6

universal-translator

๐Ÿ’ฌ i18n Translator for Go/Golang using CLDR data + pluralization rules
Go
375
star
7

log

๐Ÿ“— Simple, configurable and scalable Structured Logging for Go.
Go
292
star
8

locales

๐ŸŒŽ a set of locales generated from the CLDR Project which can be used independently or within an i18n package; these were built for use with, but not exclusive to https://github.com/go-playground/universal-translator
Go
267
star
9

mold

โœ‚๏ธ Is a general library to help modify or set data within data structures and other objects.
Go
225
star
10

stats

๐Ÿ“ˆ Monitors Go MemStats + System stats such as Memory, Swap and CPU and sends via UDP anywhere you want for logging etc...
Go
170
star
11

pure

๐Ÿšฑ Is a lightweight HTTP router that sticks to the std "net/http" implementation
Go
149
star
12

overalls

๐Ÿ‘–Multi-Package go project coverprofile for tools like goveralls
Go
114
star
13

colors

๐ŸŽจ Go color manipulation, conversion and printing library/utility
Go
67
star
14

assert

โ—Basic Assertion Library used along side native go testing, with building blocks for custom assertions
Go
62
star
15

errors

๐Ÿ’ฅError Context, Stack Trace, Types and Tags for full error handling and logging.
Go
61
star
16

kms

๐Ÿ”ช Is a library that aids in graceful shutdown of a process/application
Go
47
star
17

pkg

โญ pkg extends the core go packages with missing or additional functionality built in. All packages correspond to the std go package name with an additional suffix of `ext` to avoid naming conflicts.
Go
42
star
18

tz

Timezone Country and Zone data generated from timezonedb.com
Go
30
star
19

generate

๐Ÿƒruns go generate recursively on a specified path or environment variable and can filter by regex
Go
30
star
20

justdoit

simple auto-compile daemon that just works
Go
19
star
21

spoon

library + program to help making zero downtime, self-upgrading programs and servers.
Go
16
star
22

retry

๐Ÿ”„ Retry provides a set of standardized common components and abstracts away some code that normally is duplicated
Go
16
star
23

ansi

โฌ› ansi contains a bunch of constants and possibly additional terminal related functionality in the future.
Go
14
star
24

sensitive

provides base types who's values should never be seen by the human eye, but still used for configuration.
Go
14
star
25

ksql

a JSON data expression lexer, parser, cli and library
Go
13
star
26

backoff

:bowtie: Backoff uses an exponential backoff algorithm to backoff between retries with optional auto-tuning functionality.
Go
12
star
27

cache

Contains multiple in-memory cache implementations including LRU & LFU
Go
11
star
28

assets

Asset Pipeline for Go HTML applications
Go
8
star
29

ws

๐Ÿ™Œ ws creates a hub for WebSocket connections and abstracts away allot of the boilerplate code for managing connections using Gorilla WebSocket
Go
8
star
30

itertools

Go Iteration tools with a rusty flavour
Go
7
star
31

livereload

๐Ÿ” is an asset live-reload library that allows easy registration of path & file change monitoring and notifications for https://github.com/livereload/livereload-js
JavaScript
6
star
32

relay-client-go

This package is a Go client for the Relay Job Runner https://github.com/rust-playground/relay-rs
Go
5
star
33

backoff-sys

Bare building blocks for backing off and can be used to build more complex backoff packages
Go
4
star
34

mongostore

๐Ÿ”‘ Gorilla's session store implementation using MongoDB
Go
4
star
35

bundler

Generic Bundler to concatenate any type of files using a custom left and right delimiter, i.e. css or js files
Go
4
star
36

wave

ใ€ฐ๏ธ Package wave is a thin helper layer on top of Go's net/rpc
Go
1
star