Skip to content

Commit c3205da

Browse files
committed
New DirOptions.Cache field for in-memory caching and pre-compression 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: kataras#1556 (comment) Former-commit-id: 14b48a06fb3b99287dff543932be2937a64233b9
1 parent d697426 commit c3205da

58 files changed

Lines changed: 2580 additions & 9854 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

HISTORY.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,17 +373,38 @@ Other Improvements:
373373

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

376+
- 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:
377+
378+
```go
379+
var dirOpts = DirOptions{
380+
// [...other options]
381+
Cache: DirCacheOptions{
382+
Enable: true,
383+
// Don't compress files smaller than 300 bytes.
384+
CompressMinSize: 300,
385+
// Ignore compress already compressed file types
386+
// (some images and pdf).
387+
CompressIgnore: iris.MatchImagesAssets,
388+
// Gzip, deflate, br(brotli), snappy.
389+
Encodings: []string{"gzip", "deflate", "br", "snappy"},
390+
// Log to the stdout the total reduced file size.
391+
Verbose: 1,
392+
},
393+
}
394+
```
395+
376396
- 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):
377397

378398
```go
379-
var opts = iris.DirOptions{
380-
IndexName: "/index.html",
381-
PushTargetsRegexp: map[string]*regexp.Regexp{
399+
var dirOpts = iris.DirOptions{
400+
// [...other options]
401+
IndexName: "/index.html",
402+
PushTargetsRegexp: map[string]*regexp.Regexp{
382403
"/": regexp.MustCompile("((.*).js|(.*).css|(.*).ico)$"),
383404
// OR:
384-
// "/": iris.MatchCommonAssets,
385-
},
386-
Compress: true,
405+
// "/": iris.MatchCommonAssets,
406+
},
407+
Compress: true,
387408
}
388409
```
389410

@@ -492,7 +513,7 @@ Breaking Changes:
492513
- `iris.Gzip` and `iris.GzipReader` replaced with `iris.Compression` (middleware).
493514
- `ctx.ClientSupportsGzip() bool` replaced with `ctx.ClientSupportsEncoding("gzip", "br" ...) bool`.
494515
- `ctx.GzipResponseWriter()` is **removed**.
495-
- `Party.HandleDir` now returns a list of `[]*Route` (GET and HEAD) instead of GET only.
516+
- `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.
496517
- `Context.OnClose` and `Context.OnCloseConnection` now both accept an `iris.Handler` instead of a simple `func()` as their callback.
497518
- `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.
498519
- 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).

_examples/auth/cors/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141
app.WrapRouter(c.ServeHTTP)
4242

4343
// Serve ./public/index.html, main.js.
44-
app.HandleDir("/", "./public")
44+
app.HandleDir("/", iris.Dir("./public"))
4545

4646
// Register routes here...
4747
app.Get("/data", listData)

_examples/bootstrapper/bootstrap/bootstrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (b *Bootstrapper) Bootstrap() *Bootstrapper {
108108

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

113113
// middleware, after static files
114114
b.Use(recover.New())

_examples/desktop/webview/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package main
22

33
import (
44
"github.com/kataras/iris/v12"
5-
"github.com/zserge/webview"
5+
"github.com/webview/webview"
66
)
77

88
const addr = "127.0.0.1:8080"
@@ -18,7 +18,7 @@ const addr = "127.0.0.1:8080"
1818
#
1919
#
2020
# Note: if you see "use option -std=c99 or -std=gnu99 to compile your code"
21-
# please refer to: https://github.com/zserge/webview/issues/188
21+
# please refer to: https://github.com/webview/webview/issues/188
2222
*/
2323
func main() {
2424
go runServer()

_examples/dropzonejs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func main() {
101101
app.RegisterView(iris.HTML("./views", ".html"))
102102

103103
// Make the /public route path to statically serve the ./public/... contents
104-
app.HandleDir("/public", "./public")
104+
app.HandleDir("/public", iris.Dir("./public"))
105105

106106
// Render the actual form
107107
// GET: http://localhost:8080

_examples/dropzonejs/README_PART2.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func main() {
168168
app := iris.New()
169169
app.RegisterView(iris.HTML("./views", ".html"))
170170

171-
app.HandleDir("/public", "./public")
171+
app.HandleDir("/public", iris.Dir("./public"))
172172

173173
app.Get("/", func(ctx iris.Context) {
174174
ctx.View("upload.html")

_examples/dropzonejs/src/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func main() {
121121
app := iris.New()
122122
app.RegisterView(iris.HTML("./views", ".html"))
123123

124-
app.HandleDir("/public", "./public")
124+
app.HandleDir("/public", iris.Dir("./public"))
125125

126126
app.Get("/", func(ctx iris.Context) {
127127
ctx.View("upload.html")

_examples/file-server/basic/main.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ func newApp() *iris.Application {
1212
// first parameter is the request path
1313
// second is the system directory
1414
//
15-
// app.HandleDir("/css", "./assets/css")
16-
// app.HandleDir("/js", "./assets/js")
15+
// app.HandleDir("/css", iris.Dir("./assets/css"))
16+
// app.HandleDir("/js", iris.Dir("./assets/js"))
1717

1818
v1 := app.Party("/v1")
19-
v1.HandleDir("/static", "./assets", iris.DirOptions{
19+
v1.HandleDir("/static", iris.Dir("./assets"), iris.DirOptions{
2020
// Defaults to "/index.html", if request path is ending with **/*/$IndexName
2121
// then it redirects to **/*(/) which another handler is handling it,
2222
// that another handler, called index handler, is auto-registered by the framework
@@ -26,7 +26,19 @@ func newApp() *iris.Application {
2626
Compress: false,
2727
// List the files inside the current requested directory if `IndexName` not found.
2828
ShowList: false,
29-
// 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).
29+
Cache: iris.DirCacheOptions{
30+
// enable in-memory cache and pre-compress the files.
31+
Enable: true,
32+
// ignore image types (and pdf).
33+
CompressIgnore: iris.MatchImagesAssets,
34+
// do not compress files smaller than size.
35+
CompressMinSize: 300,
36+
// available encodings that will be negotiated with client's needs.
37+
Encodings: []string{"gzip", "br" /* you can also add: deflate, snappy */},
38+
},
39+
DirList: iris.DirListRich(),
40+
// If `ShowList` is true then this function will be used instead of the default
41+
// one to show the list of files of a current requested directory(dir).
3042
// DirList: func(ctx iris.Context, dirName string, dir http.File) error { ... }
3143
//
3244
// Optional validator that loops through each requested resource.

_examples/file-server/basic/main_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/kataras/iris/v12"
910
"github.com/kataras/iris/v12/httptest"
1011
)
1112

@@ -98,7 +99,7 @@ func TestHandleDirDot(t *testing.T) {
9899
"/v1/assets.system/css/main.css",
99100
}
100101
app := newApp()
101-
app.Subdomain("test").Party("/v1").HandleDir("/assets.system", "./assets.system")
102+
app.Subdomain("test").Party("/v1").HandleDir("/assets.system", iris.Dir("./assets.system"))
102103

103104
e := httptest.New(t, app, httptest.URL("http://test.example.com"))
104105
for _, u := range urls {

_examples/file-server/embedding-files-into-app/bindata.go

Lines changed: 134 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)