Skip to content

Commit

Permalink
cache (with compression) feature
Browse files Browse the repository at this point in the history
remove EmbeddedDir, as latest go-bindata has an -fs option for file system

read more at: kataras/iris#1556 (comment)
  • Loading branch information
kataras committed Jul 22, 2020
1 parent 79ac17f commit 5ce60c9
Show file tree
Hide file tree
Showing 11 changed files with 759 additions and 424 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.vscode
/_examples/test-*
/_examples/issue-*
/_examples/feature-*
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
Like [http.FileServer](https://pkg.go.dev/net/http?tab=doc#FileServer), plus the following features:

- Embedded files through [go-bindata](https://github.com/go-bindata/go-bindata)
- In-memory file system with pre-compressed files **NEW**
- HTTP/2 Push Targets on index requests
- On fly [fast](https://github.com/kataras/compress) [gzip](https://en.wikipedia.org/wiki/Gzip), [deflate](https://en.wikipedia.org/wiki/DEFLATE), [brotli](https://en.wikipedia.org/wiki/Brotli) and [snappy](https://en.wikipedia.org/wiki/Snappy_(compression)) compression based on the client's needs
- [Fast](https://github.com/kataras/compress) [gzip](https://en.wikipedia.org/wiki/Gzip), [deflate](https://en.wikipedia.org/wiki/DEFLATE), [brotli](https://en.wikipedia.org/wiki/Brotli) and [snappy](https://en.wikipedia.org/wiki/Snappy_(compression)) compression based on the client's needs
- Content [disposition](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition) and download speed limits
- Customize directory listing by a [template](https://pkg.go.dev/html/template?tab=doc#Template) file or an `index.html`
- Validator for each file per request, e.g. check permissions before serve a file
Expand All @@ -32,7 +33,7 @@ The `httpfs` package is fully compatible with the standard library. Use `FileSer
For system files you can use the [http.Dir](https://golang.org/pkg/net/http/#Dir):

```go
fileServer := httpfs.FileServer(http.Dir("./assets"), httpfs.DefaultOptions)
fileServer := httpfs.FileServer(http.Dir("./assets"), httpfs.DefaultOptions)
```

Where `httpfs.DefaultOptions` looks like this:
Expand All @@ -56,10 +57,22 @@ Register the `FileServer` handler:
http.Handle("/public/", fileServer)
```

To serve files that are translated as Go code, located inside the executable program itself, use the `EmbeddedDir` instead of `http.Dir`. Based on the [generated](https://github.com/go-bindata/go-bindata) `Asset`, `AssetInfo` and `AssetNames` functions:
To serve files that are translated as Go code, inside the executable program itself, use the [generated](https://github.com/go-bindata/go-bindata) `AssetFile()` instead of `http.Dir`:

```go
fileSystem := httpfs.EmbeddedDir("./assets", Asset, AssetInfo, AssetNames)
fileServer := httpfs.FileServer(AssetFile(), httpfs.DefaultOptions)
```

To cache and compress files(gzip, deflate, snappy and brotli) before server ran, wrap any file system (embedded or physical) with the `MustAsset(http.FileSystem, CacheOptions)` function:


```go
var fileSystem http.FileSystem

// fileSystem = http.Dir("./assets")
fileSystem = AssetFile()
fileSystem = httpfs.MustCache(fileSystem, httpfs.DefaultCacheOptions)

fileServer := httpfs.FileServer(fileSystem, httpfs.DefaultOptions)
```

Expand Down
6 changes: 5 additions & 1 deletion _examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ func main() {
// http.Handle("/", fileServer)

// With a prefix, use the httpfs.StripPrefix:
fileServer := httpfs.FileServer(http.Dir("./assets"), opts)
fileSystem := http.Dir("./assets")
fileServer := httpfs.FileServer(fileSystem, opts)
// with (compressed) cache:
// fileServer := httpfs.FileServer(httpfs.MustCache(fileSystem, httpfs.DefaultCacheOptions), opts)

http.Handle("/public/", http.StripPrefix("/public/", fileServer))
// http.Handle("/", fileServer)

Expand Down
Loading

0 comments on commit 5ce60c9

Please sign in to comment.