1
0
mirror of https://github.com/kataras/iris.git synced 2026-02-06 10:45:57 +00:00

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: https://github.com/kataras/iris/issues/1556#issuecomment-661057446


Former-commit-id: 14b48a06fb3b99287dff543932be2937a64233b9
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-07-24 13:03:49 +03:00
parent d697426cb6
commit c3205dafa1
58 changed files with 2581 additions and 9855 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

View File

@@ -1 +0,0 @@
console.log("example");

File diff suppressed because one or more lines are too long

View File

@@ -4,31 +4,39 @@ import (
"github.com/kataras/iris/v12"
)
// NOTE: need different tool than the "embedding-files-into-app" example.
// How to run:
//
// Follow these steps first:
// $ go get -u github.com/kataras/bindata/cmd/bindata
// $ bindata ./assets/...
// $ go get -u github.com/go-bindata/go-bindata/v3/go-bindata
// $ go-bindata -prefix "../embedding-files-into-app/assets/" -fs ../embedding-files-into-app/assets/...
// $ go run .
// $ ./embedding-gzipped-files-into-app
// "physical" files are not used, you can delete the "assets" folder and run the example.
// Time to complete the compression and caching of [2/3] files: 31.9998ms
// Total size reduced from 156.6 kB to:
// br (22.9 kB) [85.37%]
// snappy (41.7 kB) [73.37%]
// gzip (27.9 kB) [82.16%]
// deflate (27.9 kB) [82.19%]
var dirOptions = iris.DirOptions{
IndexName: "index.html",
// The `Compress` field is ignored
// when the file is cached (when Cache.Enable is true),
// because the cache file has a map of pre-compressed contents for each encoding
// that is served based on client's accept-encoding.
Compress: true, // true or false does not matter here.
Cache: iris.DirCacheOptions{
Enable: true,
CompressIgnore: iris.MatchImagesAssets,
// Here, define the encodings that the cached files should be pre-compressed
// and served based on client's needs.
Encodings: []string{"gzip", "deflate", "br", "snappy"},
CompressMinSize: 50, // files smaller than this size will NOT be compressed.
Verbose: 1,
},
}
func newApp() *iris.Application {
app := iris.New()
// Note the `GzipAsset` and `GzipAssetNames` are different from go-bindata's `Asset`,
// do not set the `Compress` option to true, instead
// use the `AssetValidator` option to manually set the content-encoding to "gzip".
app.HandleDir("/static", "./assets", iris.DirOptions{
Asset: GzipAsset,
AssetInfo: GzipAssetInfo,
AssetNames: GzipAssetNames,
AssetValidator: func(ctx iris.Context, name string) bool {
// ctx.Header("Vary", "Accept-Encoding")
ctx.Header("Content-Encoding", "gzip")
return true
},
})
app.HandleDir("/static", AssetFile(), dirOptions)
return app
}

View File

@@ -70,7 +70,9 @@ var urls = []resource{
// and secondly if the HandleDir had successfully registered
// the routes and gave the correct response.
func TestEmbeddingGzipFilesIntoApp(t *testing.T) {
dirOptions.Cache.Verbose = 0
app := newApp()
e := httptest.New(t, app)
if runtime.GOOS != "windows" {
@@ -81,17 +83,25 @@ func TestEmbeddingGzipFilesIntoApp(t *testing.T) {
for i, u := range urls {
url := u.String()
rawContents := u.loadFromBase("./assets")
rawContents := u.loadFromBase("../embedding-files-into-app/assets")
shouldBeCompressed := int64(len(rawContents)) >= dirOptions.Cache.CompressMinSize
response := e.GET(url).Expect()
request := e.GET(url)
if shouldBeCompressed {
request.WithHeader("Accept-Encoding", "gzip")
}
response := request.Expect()
response.ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset())
response.ContentEncoding("gzip")
if shouldBeCompressed {
response.ContentEncoding("gzip")
}
if expected, got := response.Raw().StatusCode, httptest.StatusOK; expected != got {
t.Fatalf("[%d] of '%s': expected %d status code but got %d", i, url, expected, got)
}
rawBody := response.Body().Raw()
func() {
if shouldBeCompressed {
reader, err := gzip.NewReader(strings.NewReader(rawBody))
defer reader.Close()
if err != nil {
@@ -105,6 +115,11 @@ func TestEmbeddingGzipFilesIntoApp(t *testing.T) {
// they are big files, no need to check for length here.
t.Fatalf("[%d] %s, expected body to look like: '%s...%s' but got '%s...%s'", i, url, expected[:40], expected[len(rawContents)-40:], got[:40], got[len(got)-40:])
}
}()
} else {
if expected, got := rawContents, rawBody; expected != got {
t.Fatalf("[%d] %s, expected body to look like: '%s...%s' but got '%s...%s'", i, url, expected[:40], expected[len(rawContents)-40:], got[:40], got[len(got)-40:])
}
}
}
}