Skip to content

Commit

Permalink
New DirOptions.Cache field for in-memory caching and pre-compression …
Browse files Browse the repository at this point in the history
…for the fastest possible static file server

Read HISTORY.md it contains a breaking change, second parameter of HandleDir should be iris.Dir(...) instead of just a string

relative to: #1556 (comment)
  • Loading branch information
kataras committed Jul 24, 2020
1 parent fc571c8 commit d259324
Show file tree
Hide file tree
Showing 58 changed files with 2,580 additions and 9,854 deletions.
35 changes: 28 additions & 7 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,38 @@ Other Improvements:

- Fix [#1553](https://github.com/kataras/iris/issues/1553).

- New `DirOptions.Cache` to cache assets in-memory among with their compressed contents (in order to be ready to served if client ask). Learn more about this feature by reading [all #1556 comments](https://github.com/kataras/iris/issues/1556#issuecomment-661057446). Usage:

```go
var dirOpts = DirOptions{
// [...other options]
Cache: DirCacheOptions{
Enable: true,
// Don't compress files smaller than 300 bytes.
CompressMinSize: 300,
// Ignore compress already compressed file types
// (some images and pdf).
CompressIgnore: iris.MatchImagesAssets,
// Gzip, deflate, br(brotli), snappy.
Encodings: []string{"gzip", "deflate", "br", "snappy"},
// Log to the stdout the total reduced file size.
Verbose: 1,
},
}
```

- New `DirOptions.PushTargets` and `PushTargetsRegexp` to push index' assets to the client without additional requests. Inspirated by issue [#1562](https://github.com/kataras/iris/issues/1562). Example matching all `.js, .css and .ico` files (recursively):

```go
var opts = iris.DirOptions{
IndexName: "/index.html",
PushTargetsRegexp: map[string]*regexp.Regexp{
var dirOpts = iris.DirOptions{
// [...other options]
IndexName: "/index.html",
PushTargetsRegexp: map[string]*regexp.Regexp{
"/": regexp.MustCompile("((.*).js|(.*).css|(.*).ico)$"),
// OR:
// "/": iris.MatchCommonAssets,
},
Compress: true,
// "/": iris.MatchCommonAssets,
},
Compress: true,
}
```

Expand Down Expand Up @@ -492,7 +513,7 @@ Breaking Changes:
- `iris.Gzip` and `iris.GzipReader` replaced with `iris.Compression` (middleware).
- `ctx.ClientSupportsGzip() bool` replaced with `ctx.ClientSupportsEncoding("gzip", "br" ...) bool`.
- `ctx.GzipResponseWriter()` is **removed**.
- `Party.HandleDir` now returns a list of `[]*Route` (GET and HEAD) instead of GET only.
- `Party.HandleDir/iris.FileServer` now accepts a `http.FileSystem` instead of a string and returns a list of `[]*Route` (GET and HEAD) instead of GET only. Write: `app.HandleDir("/", iris.Dir("./assets"))` instead of `app.HandleDir("/", "./assets")` and `DirOptions.Asset, AssetNames, AssetInfo` removed, use `go-bindata -fs [..]` and `app.HandleDir("/", AssetFile())` instead.
- `Context.OnClose` and `Context.OnCloseConnection` now both accept an `iris.Handler` instead of a simple `func()` as their callback.
- `Context.StreamWriter(writer func(w io.Writer) bool)` changed to `StreamWriter(writer func(w io.Writer) error) error` and it's now the `Context.Request().Context().Done()` channel that is used to receive any close connection/manual cancel signals, instead of the deprecated `ResponseWriter().CloseNotify()` one. Same for the `Context.OnClose` and `Context.OnCloseConnection` methods.
- Fixed handler's error response not be respected when response recorder was used instead of the common writer. Fixes [#1531](https://github.com/kataras/iris/issues/1531). It contains a **BREAKING CHANGE** of: the new `Configuration.ResetOnFireErrorCode` field should be set **to true** in order to behave as it used before this update (to reset the contents on recorder).
Expand Down
2 changes: 1 addition & 1 deletion _examples/auth/cors/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
app.WrapRouter(c.ServeHTTP)

// Serve ./public/index.html, main.js.
app.HandleDir("/", "./public")
app.HandleDir("/", iris.Dir("./public"))

// Register routes here...
app.Get("/data", listData)
Expand Down
2 changes: 1 addition & 1 deletion _examples/bootstrapper/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (b *Bootstrapper) Bootstrap() *Bootstrapper {

// static files
b.Favicon(StaticAssets + Favicon)
b.HandleDir(StaticAssets[1:len(StaticAssets)-1], StaticAssets)
b.HandleDir("/public", iris.Dir(StaticAssets))

// middleware, after static files
b.Use(recover.New())
Expand Down
4 changes: 2 additions & 2 deletions _examples/desktop/webview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"github.com/kataras/iris/v12"
"github.com/zserge/webview"
"github.com/webview/webview"
)

const addr = "127.0.0.1:8080"
Expand All @@ -18,7 +18,7 @@ const addr = "127.0.0.1:8080"
#
#
# Note: if you see "use option -std=c99 or -std=gnu99 to compile your code"
# please refer to: https://github.com/zserge/webview/issues/188
# please refer to: https://github.com/webview/webview/issues/188
*/
func main() {
go runServer()
Expand Down
2 changes: 1 addition & 1 deletion _examples/dropzonejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func main() {
app.RegisterView(iris.HTML("./views", ".html"))

// Make the /public route path to statically serve the ./public/... contents
app.HandleDir("/public", "./public")
app.HandleDir("/public", iris.Dir("./public"))

// Render the actual form
// GET: http://localhost:8080
Expand Down
2 changes: 1 addition & 1 deletion _examples/dropzonejs/README_PART2.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))

app.HandleDir("/public", "./public")
app.HandleDir("/public", iris.Dir("./public"))

app.Get("/", func(ctx iris.Context) {
ctx.View("upload.html")
Expand Down
2 changes: 1 addition & 1 deletion _examples/dropzonejs/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))

app.HandleDir("/public", "./public")
app.HandleDir("/public", iris.Dir("./public"))

app.Get("/", func(ctx iris.Context) {
ctx.View("upload.html")
Expand Down
20 changes: 16 additions & 4 deletions _examples/file-server/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func newApp() *iris.Application {
// first parameter is the request path
// second is the system directory
//
// app.HandleDir("/css", "./assets/css")
// app.HandleDir("/js", "./assets/js")
// app.HandleDir("/css", iris.Dir("./assets/css"))
// app.HandleDir("/js", iris.Dir("./assets/js"))

v1 := app.Party("/v1")
v1.HandleDir("/static", "./assets", iris.DirOptions{
v1.HandleDir("/static", iris.Dir("./assets"), iris.DirOptions{
// Defaults to "/index.html", if request path is ending with **/*/$IndexName
// then it redirects to **/*(/) which another handler is handling it,
// that another handler, called index handler, is auto-registered by the framework
Expand All @@ -26,7 +26,19 @@ func newApp() *iris.Application {
Compress: false,
// List the files inside the current requested directory if `IndexName` not found.
ShowList: false,
// If `ShowList` is true then this function will be used instead of the default one to show the list of files of a current requested directory(dir).
Cache: iris.DirCacheOptions{
// enable in-memory cache and pre-compress the files.
Enable: true,
// ignore image types (and pdf).
CompressIgnore: iris.MatchImagesAssets,
// do not compress files smaller than size.
CompressMinSize: 300,
// available encodings that will be negotiated with client's needs.
Encodings: []string{"gzip", "br" /* you can also add: deflate, snappy */},
},
DirList: iris.DirListRich(),
// If `ShowList` is true then this function will be used instead of the default
// one to show the list of files of a current requested directory(dir).
// DirList: func(ctx iris.Context, dirName string, dir http.File) error { ... }
//
// Optional validator that loops through each requested resource.
Expand Down
3 changes: 2 additions & 1 deletion _examples/file-server/basic/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/httptest"
)

Expand Down Expand Up @@ -98,7 +99,7 @@ func TestHandleDirDot(t *testing.T) {
"/v1/assets.system/css/main.css",
}
app := newApp()
app.Subdomain("test").Party("/v1").HandleDir("/assets.system", "./assets.system")
app.Subdomain("test").Party("/v1").HandleDir("/assets.system", iris.Dir("./assets.system"))

e := httptest.New(t, app, httptest.URL("http://test.example.com"))
for _, u := range urls {
Expand Down
171 changes: 134 additions & 37 deletions _examples/file-server/embedding-files-into-app/bindata.go

Large diffs are not rendered by default.

24 changes: 16 additions & 8 deletions _examples/file-server/embedding-files-into-app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
)

// Follow these steps first:
// $ go get -u github.com/go-bindata/go-bindata/...
// $ go-bindata ./assets/...
// $ go get -u github.com/go-bindata/go-bindata/v3/go-bindata
// $ go-bindata -prefix "assets" -fs ./assets/...
// $ go run .
// "physical" files are not used, you can delete the "assets" folder and run the example.
//
Expand All @@ -15,13 +15,21 @@ func newApp() *iris.Application {
app := iris.New()
app.Logger().SetLevel("debug")

app.HandleDir("/static", "./assets", iris.DirOptions{
Asset: Asset,
AssetInfo: AssetInfo,
AssetNames: AssetNames,
ShowList: true,
})
app.HandleDir("/static", AssetFile())

/*
Or if you need to cache them inside the memory (requires the assets folder
to be located near the executable program):
app.HandleDir("/static", http.Dir("./assets"), iris.DirOptions{
IndexName: "index.html",
Cache: iris.DirCacheOptions{
Enable: true,
Encodings: []string{"gzip"},
CompressIgnore: iris.MatchImagesAssets,
CompressMinSize: 30 * iris.B,
},
})
*/
return app
}

Expand Down
Loading

0 comments on commit d259324

Please sign in to comment.