Skip to content

Instantly share code, notes, and snippets.

@fujiwara
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujiwara/5c5740ee2f613e084204 to your computer and use it in GitHub Desktop.
Save fujiwara/5c5740ee2f613e084204 to your computer and use it in GitHub Desktop.
go-bindata backend file server
$ go-bindata -prefix=public public/...
$ go build
package main
import (
"bytes"
"log"
"net/http"
"os"
"strings"
)
func main() {
http.Handle("/", http.FileServer(AssetFileSystem{}))
log.Fatal(http.ListenAndServe(":3000", nil))
}
type AssetFileSystem struct {
}
type AssetFile struct {
*bytes.Reader
os.FileInfo
}
func (fs AssetFileSystem) Open(name string) (http.File, error) {
path := name
path = strings.TrimLeft(path, "/")
log.Println("open asset", path)
data, err := Asset(path)
if err != nil {
log.Println(err)
return nil, err
}
info, _ := AssetInfo(path)
file := &AssetFile{
bytes.NewReader(data),
info,
}
return file, nil
}
func (f *AssetFile) Close() error {
return nil
}
func (f *AssetFile) Readdir(count int) ([]os.FileInfo, error) {
return []os.FileInfo{}, nil
}
func (f *AssetFile) Stat() (os.FileInfo, error) {
return f, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment