-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_bindatafs_union_test.go
81 lines (66 loc) · 1.89 KB
/
example_bindatafs_union_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package bindatafs_test
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 ExampleFileSystem_union() {
// 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; }
}