Commit 9ddab3f6 authored by ale's avatar ale
Browse files

Merge branch 'renovate/github.com-elazarl-go-bindata-assetfs-1.x' into 'master'

Update module elazarl/go-bindata-assetfs to v1.0.1

See merge request !11
parents 782d5400 a7bd422f
Pipeline #10688 passed with stages
in 1 minute and 12 seconds
# go-bindata-assetfs
Serve embedded files from [jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata) with `net/http`.
Serve embedded files from [go-bindata](https://github.com/go-bindata/go-bindata) with `net/http`.
[GoDoc](http://godoc.org/github.com/elazarl/go-bindata-assetfs)
......@@ -8,12 +8,12 @@ Serve embedded files from [jteeuwen/go-bindata](https://github.com/jteeuwen/go-b
Install with
$ go get github.com/jteeuwen/go-bindata/...
$ go get github.com/go-bindata/go-bindata/...
$ go get github.com/elazarl/go-bindata-assetfs/...
### Creating embedded data
Usage is identical to [jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata) usage,
Usage is identical to [go-bindata](https://github.com/go-bindata/go-bindata) usage,
instead of running `go-bindata` run `go-bindata-assetfs`.
The tool will create a `bindata_assetfs.go` file, which contains the embedded data.
......@@ -37,10 +37,26 @@ You can always just run the `go-bindata` tool, and then
use
import "github.com/elazarl/go-bindata-assetfs"
...
http.Handle("/",
http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data"}))
```go
import "github.com/elazarl/go-bindata-assetfs"
...
http.Handle("/",
http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data"}))
```
to serve files embedded from the `data` directory.
## SPA applications
For single page applications you can use `Fallback: "index.html"` in AssetFS context, so if route doesn't match the pattern it will fallback to file specified.
example
```go
import "github.com/elazarl/go-bindata-assetfs"
...
http.Handle("/",
http.FileServer(
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data", Fallback: "index.html"}))
```
......@@ -137,6 +137,8 @@ type AssetFS struct {
AssetInfo func(path string) (os.FileInfo, error)
// Prefix would be prepended to http requests
Prefix string
// Fallback file that is served if no other is found
Fallback string
}
func (fs *AssetFS) Open(name string) (http.File, error) {
......@@ -153,9 +155,13 @@ func (fs *AssetFS) Open(name string) (http.File, error) {
}
return NewAssetFile(name, b, timestamp), nil
}
if children, err := fs.AssetDir(name); err == nil {
return NewAssetDirectory(name, children, fs), nil
} else {
children, err := fs.AssetDir(name)
if err != nil {
if len(fs.Fallback) > 0 {
return fs.Open(fs.Fallback)
}
// If the error is not found, return an error that will
// result in a 404 error. Otherwise the server returns
// a 500 error for files not found.
......@@ -164,4 +170,6 @@ func (fs *AssetFS) Open(name string) (http.File, error) {
}
return nil, err
}
return NewAssetDirectory(name, children, fs), nil
}
// assetfs allows packages to serve static content embedded
// with the go-bindata tool with the standard net/http package.
//
// See https://github.com/jteeuwen/go-bindata for more information
// See https://github.com/go-bindata/go-bindata for more information
// about embedding binary data with go-bindata.
//
// Usage example, after running
......
......@@ -30,7 +30,7 @@ github.com/coreos/go-systemd/v22/daemon
github.com/crewjam/saml
github.com/crewjam/saml/logger
github.com/crewjam/saml/xmlenc
# github.com/elazarl/go-bindata-assetfs v1.0.0
# github.com/elazarl/go-bindata-assetfs v1.0.1
## explicit
github.com/elazarl/go-bindata-assetfs
# github.com/felixge/httpsnoop v1.0.1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment