Skip to content

Commit 1de7ca3

Browse files
authored
Merge pull request #1 from go-sharp/pre-v2
Relase v2.0.0
2 parents a9f50f8 + e8ccea0 commit 1de7ca3

26 files changed

+13527
-7787
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
16+
# Exclude generated test data
17+
testdata/gen/

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: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ package main
44

55
import (
66
"fmt"
7-
"io/ioutil"
87
"log"
9-
"mime"
108
"net/http"
11-
"path/filepath"
129
"time"
1310

1411
"github.com/pkg/browser"
1512

16-
"github.com/go-sharp/vault/example/webapp-backend/res"
13+
res "github.com/go-sharp/vault/example/webapp-backend/res"
1714
)
1815

1916
func sayHelloHandler(w http.ResponseWriter, r *http.Request) {
@@ -38,27 +35,7 @@ func main() {
3835
http.HandleFunc("/api/sayhello", sayHelloHandler)
3936
http.HandleFunc("/api/time", timeHandler)
4037

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.Load(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-
w.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(f.Name())))
60-
w.Write(data)
61-
})
38+
http.Handle("/", http.FileServer(loader))
6239

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

example/webapp-backend/res/debug_react_vault.go

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,17 @@ package res
88

99
import (
1010
"fmt"
11+
"net/http"
1112
"os"
1213
"path"
13-
"strings"
14-
"time"
1514
)
1615

17-
type memFile struct {
18-
f *os.File
19-
stat os.FileInfo
20-
path string
21-
base string
22-
}
23-
24-
func (m memFile) Size() int64 {
25-
return m.stat.Size()
26-
}
27-
28-
func (m memFile) Name() string {
29-
return m.stat.Name()
30-
}
31-
32-
func (m memFile) ModTime() time.Time {
33-
return m.stat.ModTime()
34-
}
35-
36-
func (m memFile) Path() string {
37-
return m.path
38-
}
39-
40-
func (m memFile) Read(p []byte) (n int, err error) {
41-
return m.f.Read(p)
42-
}
43-
44-
func (m memFile) Close() error {
45-
return m.f.Close()
46-
}
47-
4816
type debugLoader struct {
4917
base string
5018
}
5119

52-
func (d debugLoader) Load(name string) (File, error) {
53-
if !strings.HasPrefix(name, "/") {
54-
name = "/" + name
55-
}
56-
57-
f, err := os.Open(getFullPath(d.base, name))
58-
if err != nil {
59-
return nil, err
60-
}
61-
62-
stat, err := f.Stat()
63-
if err != nil {
64-
return nil, err
65-
}
66-
67-
return &memFile{base: d.base, path: path.Clean(strings.TrimSuffix(name, stat.Name())), f: f, stat: stat}, nil
20+
func (d debugLoader) Open(name string) (http.File, error) {
21+
return os.Open(getFullPath(d.base, name))
6822
}
6923

7024
func getFullPath(b, p string) string {

example/webapp-backend/res/release_react_vault.go

Lines changed: 311 additions & 57 deletions
Large diffs are not rendered by default.

example/webapp-backend/res/shared_react_vault.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,11 @@
55
package res
66

77
import (
8-
"errors"
9-
"io"
10-
"time"
8+
"net/http"
119
)
1210

13-
// ErrNotFound is returned if the requested file was not found.
14-
var ErrNotFound = errors.New("file not found")
15-
16-
// File is the vault abstraction of a file.
17-
type File interface {
18-
io.ReadCloser
19-
// Size returns the size of the file.
20-
Size() int64
21-
// Name returns the name of the file.
22-
Name() string
23-
// ModTime returns the modification time.
24-
ModTime() time.Time
25-
// Path is the registered path within the vault.
26-
Path() string
27-
}
28-
2911
// AssetLoader implements a function to load an asset from the vault
3012
type AssetLoader interface {
31-
// Load loads a file from the vault.
32-
Load(name string) (File, error)
13+
// Open loads a file from the vault.
14+
Open(name string) (http.File, error)
3315
}

0 commit comments

Comments
 (0)