Documentation ¶
Overview ¶
Package bindatafs provides wrapper vfs.FileSystem implementation to bridge go-bindata-generated assets to be served by http.FileServer.
Example ¶
package main import ( "fmt" "net/http" "github.com/go-serve/bindatafs" "github.com/go-serve/bindatafs/examples/example1" "golang.org/x/tools/godoc/vfs/httpfs" ) func exampleIndex(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello Index\n") } func main() { // create vfs.FileSystem implementation for // the go-bindata generated assets assetsfs := bindatafs.New( "assets://", example1.Asset, example1.AssetDir, example1.AssetInfo, ) // serve the files with http mux := http.NewServeMux() mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(httpfs.New(assetsfs)))) mux.Handle("/", http.HandlerFunc(exampleIndex)) // serve the mux http.ListenAndServe(":8080", mux) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssetDirFunc ¶
AssetDirFunc is the AssetDir() function generated by go-bindata
type AssetInfoFunc ¶
AssetInfoFunc is the AssetInfo() function generated by go-bindata
type FileReader ¶
FileReader implements vfs.ReadSeekCloser
type FileSystem ¶
type FileSystem interface { vfs.Opener Lstat(path string) (os.FileInfo, error) Stat(path string) (os.FileInfo, error) ReadDir(path string) ([]os.FileInfo, error) RootType(string) vfs.RootType String() string }
FileSystem is a copy of vfs interface FileSystem
Example ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/go-serve/bindatafs" "github.com/go-serve/bindatafs/examples/example1" "golang.org/x/tools/godoc/vfs/httpfs" ) func exampleFsIndex(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello Index\n") } func main() { // create vfs.FileSystem implementation for // the go-bindata generated assets assetsfs := bindatafs.New( "assets://", example1.Asset, example1.AssetDir, example1.AssetInfo, ) // serve the files with http mux := http.NewServeMux() mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(httpfs.New(assetsfs)))) mux.Handle("/", http.HandlerFunc(exampleFsIndex)) // production: uncomment this //http.ListenAndServe(":8080", mux) // below are for testings, can be removed for production // test the mux with httptest server server := httptest.NewServer(mux) defer server.Close() // examine the index resp, _ := http.Get(server.URL) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("%s", body) // examine an asset resp, _ = http.Get(server.URL + "/assets/hello.txt") defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) fmt.Printf("%s", body) }
Output: Hello Index Hello World
Example (Union) ¶
package main import ( "fmt" "io/ioutil" "net/http" "net/http/httptest" "github.com/go-serve/bindatafs" "github.com/go-serve/bindatafs/examples/example1" "github.com/go-serve/bindatafs/examples/example2" "golang.org/x/tools/godoc/vfs" "golang.org/x/tools/godoc/vfs/httpfs" ) func exampleUnionIndex(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "Hello Index\n") } func main() { // create vfs.FileSystem implementation for // the go-bindata generated assets assetsfs1 := bindatafs.New( "assets1://", example1.Asset, example1.AssetDir, example1.AssetInfo, ) assetsfs2 := bindatafs.New( "assets2://", example2.Asset, example2.AssetDir, example2.AssetInfo, ) // compose 2 assets set into the same // namespace assetsfs := vfs.NameSpace{} assetsfs.Bind("/", assetsfs2, "/", vfs.BindAfter) assetsfs.Bind("/", assetsfs1, "/", vfs.BindAfter) // serve the files with http mux := http.NewServeMux() mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(httpfs.New(assetsfs)))) mux.Handle("/", http.HandlerFunc(exampleUnionIndex)) // production: uncomment this //http.ListenAndServe(":8080", mux) // below are for testings, can be removed for production // test the mux with httptest server server := httptest.NewServer(mux) defer server.Close() // examine the index resp, _ := http.Get(server.URL) defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) fmt.Printf("%s", body) // examine an asset resp, _ = http.Get(server.URL + "/assets/hello.txt") defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) fmt.Printf("%s", body) // examine an asset resp, _ = http.Get(server.URL + "/assets/css/style.css") defer resp.Body.Close() body, _ = ioutil.ReadAll(resp.Body) fmt.Printf("%s", body) }
Output: Hello Index Hello CSS Assets body { background-color: #AFA; }
func New ¶
func New(name string, Asset AssetFunc, AssetDir AssetDirFunc, AssetInfo AssetInfoFunc) FileSystem
New returns a FileSystem implementation of the given go-bindata generated assets
Click to show internal directories.
Click to hide internal directories.