Former-commit-id: 6e0708f87f6ee9c6efd92f7677595feb8910988a
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-11 01:01:28 +03:00
parent 54bef51eb9
commit 3574816e1d
3 changed files with 924 additions and 912 deletions

View File

@ -6,6 +6,7 @@
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
@ -13,6 +14,7 @@ import (
"time" "time"
) )
type gzipAsset struct { type gzipAsset struct {
bytes []byte bytes []byte
info gzipFileInfoEx info gzipFileInfoEx
@ -999,6 +1001,8 @@ func gzipBindataAssetsjsjquery211js() (*gzipAsset, error) {
return a, nil return a, nil
} }
// GzipAsset loads and returns the asset for the given name. // GzipAsset loads and returns the asset for the given name.
// It returns an error if the asset could not be found or // It returns an error if the asset could not be found or
// could not be loaded. // could not be loaded.
@ -1059,6 +1063,7 @@ var _gzipbindata = map[string]func() (*gzipAsset, error){
"assets/js/jquery-2.1.1.js": gzipBindataAssetsjsjquery211js, "assets/js/jquery-2.1.1.js": gzipBindataAssetsjsjquery211js,
} }
// GzipAssetDir returns the file names below a certain // GzipAssetDir returns the file names below a certain
// directory embedded in the file by bindata. // directory embedded in the file by bindata.
// For example if you run bindata on data/... and data contains the // For example if you run bindata on data/... and data contains the
@ -1102,6 +1107,7 @@ func GzipAssetDir(name string) ([]string, error) {
return rv, nil return rv, nil
} }
type gzipBintree struct { type gzipBintree struct {
Func func() (*gzipAsset, error) Func func() (*gzipAsset, error)
Children map[string]*gzipBintree Children map[string]*gzipBintree

View File

@ -9,19 +9,25 @@ import (
// Follow these steps first: // Follow these steps first:
// $ go get -u github.com/kataras/bindata/cmd/bindata // $ go get -u github.com/kataras/bindata/cmd/bindata
// $ bindata ./assets/... // $ bindata ./assets/...
// $ go build // $ go run .
// $ ./embedding-gziped-files-into-app // $ ./embedding-gziped-files-into-app
// "physical" files are not used, you can delete the "assets" folder and run the example. // "physical" files are not used, you can delete the "assets" folder and run the example.
func newApp() *iris.Application { func newApp() *iris.Application {
app := iris.New() app := iris.New()
// Note the `GzipAsset` and `GzipAssetNames` are different from `go-bindata`'s `Asset`, // Note the `GzipAsset` and `GzipAssetNames` are different from go-bindata's `Asset`,
// do not set the `Gzip` option to true, it's already managed by the kataras/bindata. // 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{ app.HandleDir("/static", "./assets", iris.DirOptions{
Asset: GzipAsset, Asset: GzipAsset,
AssetInfo: GzipAssetInfo, AssetInfo: GzipAssetInfo,
AssetNames: GzipAssetNames, AssetNames: GzipAssetNames,
AssetValidator: func(ctx iris.Context, name string) bool {
ctx.Header("Vary", "Accept-Encoding")
ctx.Header("Content-Encoding", "gzip")
return true
},
}) })
return app return app
} }

View File

@ -85,7 +85,7 @@ func TestEmbeddingGzipFilesIntoApp(t *testing.T) {
response := e.GET(url).Expect() response := e.GET(url).Expect()
response.ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset()) response.ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset())
response.ContentEncoding("gzip")
if expected, got := response.Raw().StatusCode, httptest.StatusOK; expected != got { 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) t.Fatalf("[%d] of '%s': expected %d status code but got %d", i, url, expected, got)
} }