bindatafs

package module
v0.0.0-...-47e1640 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2023 License: MIT Imports: 6 Imported by: 1

README

bindatafs

Documentations GitHub Workflow results Go Report Card Coverage Status

bindatafs helps to serve go-bindata-generated assets with http.FileServer.

Install

go get -u github.com/go-serve/bindatafs

Example


package main

import (
    "net/http"

    "github.com/go-serve/bindatafs"
    "golang.org/x/tools/godoc/vfs/httpfs"
)

// FileSystem returns a Filesystem implementation for the given assets
func FileSystem() bindatafs.FileSystem {
    // assume you have Asset, AssetDir, AssetInfo are generated by go-bindata
    return bindatafs.New("assets://", Asset, AssetDir, AssetInfo)
}

func main() {
    handler := http.FileServer(httpfs.New(FileSystem()))
    http.ListenAndServe(":8080", handler)
}

For more examples, please read the Documentations.

Author

This software is written by Koala Yeung (koalay at gmail.com).

Licence

This software is licenced under the MIT License. You may obtain a copy of the licence in the LICENSE.md file in this repository.

Contributing and Bug Report

Pull requests are welcomed. Please read the CONTRIBUTING.md for details.

Bug reports are always welcome to our issue tracker.

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

type AssetDirFunc func(name string) ([]string, error)

AssetDirFunc is the AssetDir() function generated by go-bindata

type AssetFunc

type AssetFunc func(name string) ([]byte, error)

AssetFunc is the Assets() function generated by go-bindata

type AssetInfoFunc

type AssetInfoFunc func(name string) (os.FileInfo, error)

AssetInfoFunc is the AssetInfo() function generated by go-bindata

type FileReader

type FileReader struct {
	*bytes.Reader
}

FileReader implements vfs.ReadSeekCloser

func (*FileReader) Close

func (r *FileReader) Close() error

Close implements io.Closer

func (*FileReader) Read

func (r *FileReader) Read(p []byte) (int, error)

Read implements io.Reader

func (*FileReader) Seek

func (r *FileReader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker

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

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL