From c4e3e01a417b50e77fe247acef65261ad2a45daf Mon Sep 17 00:00:00 2001 From: joegasewicz Date: Tue, 12 Apr 2022 20:48:33 +0100 Subject: [PATCH 1/2] Cannot declare multiple directories with go-bindata-assetfs #58 --- README.md | 22 ++++++++++++++++++++++ go-bindata-assetfs/main.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/README.md b/README.md index b326dca..907f796 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 containg 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 0c7f42c..bfeb1d8 100644 --- a/go-bindata-assetfs/main.go +++ b/go-bindata-assetfs/main.go @@ -101,6 +101,20 @@ func assetFS() http.FileSystem { return http.Dir(k) } panic("unreachable") +}`) + fmt.Fprintln(out, ` +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, ` @@ -109,6 +123,20 @@ func assetFS() *assetfs.AssetFS { return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: k} } panic("unreachable") +}`) + fmt.Fprintln(out, ` +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). From 17c5444c0a900e352e8deb68035734a2ca195ac9 Mon Sep 17 00:00:00 2001 From: joegasewicz Date: Wed, 13 Apr 2022 09:43:55 +0100 Subject: [PATCH 2/2] typo --- .gitignore | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) 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 907f796..9bdd9fe 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ http.FileServer( ## 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 containg a `dist/..` & `public/...` 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/...