Browse code

add(github.com/elazarl/go-bindata-assetfs): 4e003f5e7162b67b84ea0f6117218f9c6f8fd2df

Jessica Forrester authored on 2014/09/20 05:55:21
Showing 5 changed files
... ...
@@ -363,6 +363,10 @@
363 363
 			"Rev": "8f74c29bc59d7f0b4fc6cfd6fa59f01532be9f9f"
364 364
 		},
365 365
 		{
366
+			"ImportPath": "github.com/elazarl/go-bindata-assetfs",
367
+			"Rev": "4e003f5e7162b67b84ea0f6117218f9c6f8fd2df"
368
+		},
369
+		{      
366 370
 			"ImportPath": "github.com/fsouza/go-dockerclient",
367 371
 			"Comment": "0.2.1-241-g0dbb508",
368 372
 			"Rev": "0dbb508e94dd899a6743d035d8f249c7634d26da"
369 373
new file mode 100644
... ...
@@ -0,0 +1,23 @@
0
+Copyright (c) 2014, Elazar Leibovich
1
+All rights reserved.
2
+
3
+Redistribution and use in source and binary forms, with or without
4
+modification, are permitted provided that the following conditions are met:
5
+
6
+* Redistributions of source code must retain the above copyright notice, this
7
+  list of conditions and the following disclaimer.
8
+
9
+* Redistributions in binary form must reproduce the above copyright notice,
10
+  this list of conditions and the following disclaimer in the documentation
11
+  and/or other materials provided with the distribution.
12
+
13
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
17
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0 23
new file mode 100644
... ...
@@ -0,0 +1,18 @@
0
+go-bindata-http
1
+===============
2
+
3
+Serve embedded files from [jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata) with `net/http`.
4
+
5
+[GoDoc](http://godoc.org/github.com/elazarl/go-bindata-assetfs)
6
+
7
+After running
8
+
9
+    $ go-bindata data/...
10
+
11
+Use
12
+
13
+     http.Handle("/",
14
+        http.FileServer(
15
+        &assetfs.AssetFS{Asset, AssetDir, "data"}))
16
+
17
+to serve files embedded from the `data` directory.
0 18
new file mode 100644
... ...
@@ -0,0 +1,141 @@
0
+package assetfs
1
+
2
+import (
3
+	"bytes"
4
+	"errors"
5
+	"fmt"
6
+	"io"
7
+	"io/ioutil"
8
+	"net/http"
9
+	"os"
10
+	"path"
11
+	"path/filepath"
12
+	"time"
13
+)
14
+
15
+// FakeFile implements os.FileInfo interface for a given path and size
16
+type FakeFile struct {
17
+	// Path is the path of this file
18
+	Path string
19
+	// Dir marks of the path is a directory
20
+	Dir bool
21
+	// Len is the length of the fake file, zero if it is a directory
22
+	Len int64
23
+}
24
+
25
+func (f *FakeFile) Name() string {
26
+	_, name := filepath.Split(f.Path)
27
+	return name
28
+}
29
+
30
+func (f *FakeFile) Mode() os.FileMode {
31
+	mode := os.FileMode(0644)
32
+	if f.Dir {
33
+		return mode | os.ModeDir
34
+	}
35
+	return mode
36
+}
37
+
38
+func (f *FakeFile) ModTime() time.Time {
39
+	return time.Unix(0, 0)
40
+}
41
+
42
+func (f *FakeFile) Size() int64 {
43
+	return f.Len
44
+}
45
+
46
+func (f *FakeFile) IsDir() bool {
47
+	return f.Mode().IsDir()
48
+}
49
+
50
+func (f *FakeFile) Sys() interface{} {
51
+	return nil
52
+}
53
+
54
+// AssetFile implements http.File interface for a no-directory file with content
55
+type AssetFile struct {
56
+	*bytes.Reader
57
+	io.Closer
58
+	FakeFile
59
+}
60
+
61
+func NewAssetFile(name string, content []byte) *AssetFile {
62
+	return &AssetFile{
63
+		bytes.NewReader(content),
64
+		ioutil.NopCloser(nil),
65
+		FakeFile{name, false, int64(len(content))}}
66
+}
67
+
68
+func (f *AssetFile) Readdir(count int) ([]os.FileInfo, error) {
69
+	return nil, errors.New("not a directory")
70
+}
71
+
72
+func (f *AssetFile) Stat() (os.FileInfo, error) {
73
+	return f, nil
74
+}
75
+
76
+// AssetDirectory implements http.File interface for a directory
77
+type AssetDirectory struct {
78
+	AssetFile
79
+	ChildrenRead int
80
+	Children     []os.FileInfo
81
+}
82
+
83
+func NewAssetDirectory(name string, children []string, fs *AssetFS) *AssetDirectory {
84
+	fileinfos := make([]os.FileInfo, 0, len(children))
85
+	for _, child := range children {
86
+		_, err := fs.AssetDir(filepath.Join(name, child))
87
+		fileinfos = append(fileinfos, &FakeFile{child, err == nil, 0})
88
+	}
89
+	return &AssetDirectory{
90
+		AssetFile{
91
+			bytes.NewReader(nil),
92
+			ioutil.NopCloser(nil),
93
+			FakeFile{name, true, 0},
94
+		},
95
+		0,
96
+		fileinfos}
97
+}
98
+
99
+func (f *AssetDirectory) Readdir(count int) ([]os.FileInfo, error) {
100
+	fmt.Println(f, count)
101
+	if count <= 0 {
102
+		return f.Children, nil
103
+	}
104
+	if f.ChildrenRead+count > len(f.Children) {
105
+		count = len(f.Children) - f.ChildrenRead
106
+	}
107
+	rv := f.Children[f.ChildrenRead : f.ChildrenRead+count]
108
+	f.ChildrenRead += count
109
+	return rv, nil
110
+}
111
+
112
+func (f *AssetDirectory) Stat() (os.FileInfo, error) {
113
+	return f, nil
114
+}
115
+
116
+// AssetFS implements http.FileSystem, allowing
117
+// embedded files to be served from net/http package.
118
+type AssetFS struct {
119
+	// Asset should return content of file in path if exists
120
+	Asset func(path string) ([]byte, error)
121
+	// AssetDir should return list of files in the path
122
+	AssetDir func(path string) ([]string, error)
123
+	// Prefix would be prepended to http requests
124
+	Prefix string
125
+}
126
+
127
+func (fs *AssetFS) Open(name string) (http.File, error) {
128
+	name = path.Join(fs.Prefix, name)
129
+	if len(name) > 0 && name[0] == '/' {
130
+		name = name[1:]
131
+	}
132
+	if children, err := fs.AssetDir(name); err == nil {
133
+		return NewAssetDirectory(name, children, fs), nil
134
+	}
135
+	b, err := fs.Asset(name)
136
+	if err != nil {
137
+		return nil, err
138
+	}
139
+	return NewAssetFile(name, b), nil
140
+}
0 141
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+// assetfs allows packages to serve static content embedded
1
+// with the go-bindata tool with the standard net/http package.
2
+//
3
+// See https://github.com/jteeuwen/go-bindata for more information
4
+// about embedding binary data with go-bindata.
5
+//
6
+// Usage example, after running
7
+//    $ go-bindata data/...
8
+// use:
9
+//     http.Handle("/",
10
+//        http.FileServer(
11
+//        &assetfs.AssetFS{Asset, AssetDir, "data"}))
12
+package assetfs