diff --git a/.gitignore b/.gitignore index 1646a98..233a7d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.vscode .idea vendor tags diff --git a/README.md b/README.md index b326dca..9bdd9fe 100644 --- a/README.md +++ b/README.md @@ -60,3 +60,25 @@ http.Handle("/", http.FileServer( &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data", Fallback: "index.html"})) ``` + +## Specifying multiple directories + +To generate binary data from calling the `go-bindata-assetfs` cmd when declaring multiple directories, then you can use the `AssetDIR(dir string)` function. For example, running the following command inside a directory contaning a `dist/..` & `public/...` directories: + +``` +$ go-bindata-assetfs dist/... public/... +``` + +Our server code might look like this using the `AssetDIR(dir string)` function + +```go +func main() { + r := mux.NewRouter() // Example uses "github.com/gorilla/mux" + // Here we want to serve static content from 2 directories + r.PathPrefix("/dist").Handler(http.StripPrefix("/dist", http.FileServer(AssetDIR("dist")))) + r.PathPrefix("/public").Handler(http.StripPrefix("/public", http.FileServer(AssetDIR("public")))) + // rest of server code... + server := &http.Server{ Addr: ":7777", Handler: r, } + server.ListenAndServe() +} +``` \ No newline at end of file diff --git a/go-bindata-assetfs/main.go b/go-bindata-assetfs/main.go index 7c49bb0..d8c926f 100644 --- a/go-bindata-assetfs/main.go +++ b/go-bindata-assetfs/main.go @@ -106,6 +106,17 @@ func assetFS() http.FileSystem { func AssetFS() http.FileSystem { return assetFS() }`) + fmt.Fprintln(out, ` +// AssetDIR similiar to assetFS() but requires a directory name +func AssetDIR(dir string) http.FileSystem { + for k := range _bintree.Children { + if k == dir { + return http.Dir(k) + } + } + panic("No asset with name " + dir) +}`) + } else { fmt.Fprintln(out, ` func assetFS() *assetfs.AssetFS { @@ -118,6 +129,17 @@ func assetFS() *assetfs.AssetFS { func AssetFS() *assetfs.AssetFS { return assetFS() }`) + fmt.Fprintln(out, ` +// AssetDIR similiar to assetFS() but requires a directory name +func AssetDIR(dir string) *assetfs.AssetFS { + for k := range _bintree.Children { + if k == dir { + return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: k} + } + } + panic("No asset with name " + dir) +}`) + } // Close files BEFORE remove calls (don't use defer). in.Close()