Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot declare multiple directories with go-bindata-assetfs #58 #59

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode
.idea
vendor
tags
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
```
22 changes: 22 additions & 0 deletions go-bindata-assetfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Copy link
Contributor Author

@joegasewicz joegasewicz Apr 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bindata already generates a function with name of AssetDir maybe AssetDIR is too similar?

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()
Expand Down