Skip to content

Commit e8ccea0

Browse files
committed
Finalize release 2.0.0
- Update example with newest node packages - Generate example with vault-cli v2.0.0 - Fix module definition - Update readme
1 parent 375d14c commit e8ccea0

File tree

11 files changed

+12707
-7737
lines changed

11 files changed

+12707
-7737
lines changed

README.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Vault is a simple tool to embed resource and asset files into a go binary. It generates go source files containing encoded and compressed resource files. The generated source code provides an api to retrieve the embedded resources without any additional dependencies and therefore the vault package is only needed to generate those source files.
44

5+
## Requirements
6+
7+
go 1.9.7+
8+
59
## Installation
610

711
Install tool with `go get`:
@@ -116,7 +120,7 @@ import (
116120
func main() {
117121
// Create a loader (default: New{source folder name}Loader() -> can be changed with ResourceNameOption)
118122
loader := res.NewDistLoader()
119-
f, err := loader.Load("/bg.jpg")
123+
f, err := loader.Open("/bg.jpg")
120124
if err != nil {
121125
log.Fatalln(err)
122126
}
@@ -136,7 +140,7 @@ func main() {
136140
log.Fatalln(err)
137141
}
138142

139-
f2, err := loader.Load("/js/app.js")
143+
f2, err := loader.Open("/js/app.js")
140144
if err != nil {
141145
log.Fatalln(err)
142146
}
@@ -162,28 +166,32 @@ The asset loader implements only one method:
162166
```go
163167
// AssetLoader implements a function to load an asset from the vault
164168
type AssetLoader interface {
165-
// Load loads a file from the vault.
166-
Load(name string) (File, error)
169+
// Open loads a file from the vault.
170+
Open(name string) (File, error)
167171
}
168172
```
169173

170-
Load returns a file instance with the following interface or an error:
174+
Load returns a file instance with the following interface (see http.File) or an error:
171175

172176
```go
173-
// File is the vault abstraction of a file.
177+
// File is the interface definition in the http package.
174178
type File interface {
175-
io.ReadCloser
176-
// Size returns the size of the file.
177-
Size() int64
178-
// Name returns the name of the file.
179-
Name() string
180-
// ModTime returns the modification time.
181-
ModTime() time.Time
182-
// Path is the registered path within the vault.
183-
Path() string
179+
io.Closer
180+
io.Reader
181+
io.Seeker
182+
Readdir(count int) ([]os.FileInfo, error)
183+
Stat() (os.FileInfo, error)
184184
}
185185
```
186186

187+
#### Use Loader in the http.FileServer handler
188+
189+
The asset loader implements the `http.FileSystem` interface and can be used with the `http.FileServer` handler. Just pass the loader to the _FileServer_ function as follows:
190+
191+
```go
192+
http.Handle("/", http.FileServer(res.NewDistLoader()))
193+
```
194+
187195
#### Development Mode
188196

189197
Run or build a program with `go run -tags debug main.go` to enable the development mode. In development mode the loader bypasses the embedded files and reads directly from the source directory. Therefore it is important to run the program in the same folder as the vault-cli tool was invoked. Otherwise invoke vault-cli with the flag `-rp` and specify the relative path to the source directory from the directory where the program will be executed. For example (folder structure as above):

example/webapp-backend/main.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate vault-cli -s -n react ../webapp-frontend/build ./resv2
1+
//go:generate vault-cli -s -n react ../webapp-frontend/build ./res
22

33
package main
44

@@ -10,7 +10,7 @@ import (
1010

1111
"github.com/pkg/browser"
1212

13-
res "github.com/go-sharp/vault/example/webapp-backend/resv2"
13+
res "github.com/go-sharp/vault/example/webapp-backend/res"
1414
)
1515

1616
func sayHelloHandler(w http.ResponseWriter, r *http.Request) {
@@ -35,31 +35,7 @@ func main() {
3535
http.HandleFunc("/api/sayhello", sayHelloHandler)
3636
http.HandleFunc("/api/time", timeHandler)
3737

38-
http.Handle("/files/", http.StripPrefix("/files/", http.FileServer(loader)))
39-
http.Handle("/files2/", http.StripPrefix("/files2/", http.FileServer(http.Dir("../webapp-frontend/build"))))
40-
41-
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
42-
// fp := r.URL.Path
43-
// if fp == "/" {
44-
// fp = "/index.html"
45-
// }
46-
// log.Printf("requesting: %v...", r.URL.Path)
47-
48-
// f, err := loader.Open(fp)
49-
// if err != nil {
50-
// w.WriteHeader(http.StatusNotFound)
51-
// return
52-
// }
53-
// data, err := ioutil.ReadAll(f)
54-
// if err != nil {
55-
// w.WriteHeader(http.StatusInternalServerError)
56-
// }
57-
// defer f.Close()
58-
59-
// fi, _ := f.Stat()
60-
// w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fi.Name())))
61-
// w.Write(data)
62-
// })
38+
http.Handle("/", http.FileServer(loader))
6339

6440
log.Println("webapp started, listening on port :8080...")
6541
browser.OpenURL("http://localhost:8080")

example/webapp-backend/res/debug_react_vault.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,16 @@ package res
88

99
import (
1010
"fmt"
11+
"net/http"
1112
"os"
1213
"path"
13-
"strings"
1414
)
1515

1616
type debugLoader struct {
1717
base string
1818
}
1919

20-
func (d debugLoader) Open(name string) (File, error) {
21-
if !strings.HasPrefix(name, "/") {
22-
name = "/" + name
23-
}
24-
20+
func (d debugLoader) Open(name string) (http.File, error) {
2521
return os.Open(getFullPath(d.base, name))
2622
}
2723

example/webapp-backend/res/release_react_vault.go

Lines changed: 199 additions & 93 deletions
Large diffs are not rendered by default.

example/webapp-backend/res/shared_react_vault.go

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,11 @@
55
package res
66

77
import (
8-
"errors"
9-
"fmt"
10-
"io"
11-
"os"
12-
"sort"
13-
"strings"
8+
"net/http"
149
)
1510

16-
// ErrNotFound is returned if the requested file was not found.
17-
var ErrNotFound = errors.New("file not found")
18-
19-
// File is the vault abstraction of a file.
20-
type File interface {
21-
io.Closer
22-
io.Reader
23-
io.Seeker
24-
Readdir(count int) ([]os.FileInfo, error)
25-
Stat() (os.FileInfo, error)
26-
}
27-
2811
// AssetLoader implements a function to load an asset from the vault
2912
type AssetLoader interface {
3013
// Open loads a file from the vault.
31-
Open(name string) (File, error)
32-
}
33-
34-
type assetMap map[string]memFile
35-
36-
func createDirFile(path string, assets assetMap) File {
37-
md := memDir{dir: path}
38-
dirs := map[string]*memDir{}
39-
40-
for k, v := range assets {
41-
if k == md.dir {
42-
md.files = append(md.files, &v)
43-
continue
44-
}
45-
if strings.HasPrefix(k, md.dir) {
46-
p := strings.TrimLeft(k, md.dir)
47-
if p[0] == '/' {
48-
p = p[1:]
49-
}
50-
51-
idx := len(p)
52-
for i := 0; i < len(p); i++ {
53-
if p[i] == '/' {
54-
idx = i
55-
break
56-
}
57-
}
58-
p = p[:idx]
59-
if dir, ok := dirs[p]; ok {
60-
dir.size += v.size
61-
} else {
62-
newDir := memDir{dir: fmt.Sprintf("%v/%v", md.dir, p), size: v.size}
63-
md.files = append(md.files, newDir)
64-
dirs[p] = &newDir
65-
}
66-
}
67-
}
68-
69-
sort.Slice(md.files, func(i int, j int) bool {
70-
return md.files[i].Name() < md.files[j].Name()
71-
})
72-
return &md
14+
Open(name string) (http.File, error)
7315
}

0 commit comments

Comments
 (0)