-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_bindatafs_fs_test.go
59 lines (47 loc) · 1.3 KB
/
example_bindatafs_fs_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
package bindatafs_test
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 ExampleFileSystem() {
// 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
}