• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

go-bindata-assetfs/H09-May-2020-

LICENSEH A D09-May-20201.3 KiB

README.mdH A D09-May-20201.6 KiB

assetfs.goH A D09-May-20203.9 KiB

doc.goH A D09-May-2020461

README.md

1# go-bindata-assetfs
2
3Serve embedded files from [go-bindata](https://github.com/go-bindata/go-bindata) with `net/http`.
4
5[GoDoc](http://godoc.org/github.com/elazarl/go-bindata-assetfs)
6
7### Installation
8
9Install with
10
11    $ go get github.com/go-bindata/go-bindata/...
12    $ go get github.com/elazarl/go-bindata-assetfs/...
13
14### Creating embedded data
15
16Usage is identical to [go-bindata](https://github.com/go-bindata/go-bindata) usage,
17instead of running `go-bindata` run `go-bindata-assetfs`.
18
19The tool will create a `bindata_assetfs.go` file, which contains the embedded data.
20
21A typical use case is
22
23    $ go-bindata-assetfs data/...
24
25### Using assetFS in your code
26
27The generated file provides an `assetFS()` function that returns a `http.Filesystem`
28wrapping the embedded files. What you usually want to do is:
29
30    http.Handle("/", http.FileServer(assetFS()))
31
32This would run an HTTP server serving the embedded files.
33
34## Without running binary tool
35
36You can always just run the `go-bindata` tool, and then
37
38use
39
40```go
41import "github.com/elazarl/go-bindata-assetfs"
42...
43http.Handle("/",
44http.FileServer(
45&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data"}))
46```
47
48to serve files embedded from the `data` directory.
49
50## SPA applications
51
52For 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.
53
54example
55
56```go
57import "github.com/elazarl/go-bindata-assetfs"
58...
59http.Handle("/",
60http.FileServer(
61&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data", Fallback: "index.html"}))
62```
63