Last active
August 29, 2015 14:13
-
-
Save fujiwara/5c5740ee2f613e084204 to your computer and use it in GitHub Desktop.
go-bindata backend file server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ go-bindata -prefix=public public/... | |
$ go build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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