mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
DirOptions.PushTargets: add example for https://github.com/kataras/iris/issues/1562
Former-commit-id: 940f07fdb1184266dc315441fb91afa5754092fa
This commit is contained in:
7225
_examples/file-server/embedding-gzipped-files-into-app/assets/css/bootstrap.min.css
vendored
Normal file
7225
_examples/file-server/embedding-gzipped-files-into-app/assets/css/bootstrap.min.css
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
1
_examples/file-server/embedding-gzipped-files-into-app/assets/js/jquery-2.1.1.js
vendored
Normal file
1
_examples/file-server/embedding-gzipped-files-into-app/assets/js/jquery-2.1.1.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
console.log("example");
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// NOTE: need different tool than the "embedding-files-into-app" example.
|
||||
//
|
||||
// Follow these steps first:
|
||||
// $ go get -u github.com/kataras/bindata/cmd/bindata
|
||||
// $ bindata ./assets/...
|
||||
// $ go run .
|
||||
// $ ./embedding-gzipped-files-into-app
|
||||
// "physical" files are not used, you can delete the "assets" folder and run the example.
|
||||
|
||||
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
|
||||
},
|
||||
})
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
|
||||
// http://localhost:8080/static/css/bootstrap.min.css
|
||||
// http://localhost:8080/static/js/jquery-2.1.1.js
|
||||
// http://localhost:8080/static/favicon.ico
|
||||
app.Listen(":8080")
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/v12/httptest"
|
||||
"github.com/klauspost/compress/gzip"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
// content types that are used in the ./assets,
|
||||
// we could use the detectContentType that iris do but it's better
|
||||
// to do it manually so we can test if that returns the correct result on embedding files.
|
||||
func (r resource) contentType() string {
|
||||
switch filepath.Ext(r.String()) {
|
||||
case ".js":
|
||||
return "text/javascript"
|
||||
case ".css":
|
||||
return "text/css"
|
||||
case ".ico":
|
||||
return "image/x-icon"
|
||||
case ".html":
|
||||
return "text/html"
|
||||
default:
|
||||
return "text/plain"
|
||||
}
|
||||
}
|
||||
|
||||
func (r resource) String() string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func (r resource) strip(strip string) string {
|
||||
s := r.String()
|
||||
return strings.TrimPrefix(s, strip)
|
||||
}
|
||||
|
||||
func (r resource) loadFromBase(dir string) string {
|
||||
filename := r.String()
|
||||
|
||||
filename = r.strip("/static")
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
result := string(b)
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
result = strings.Replace(result, "\n", "\r\n", -1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
var urls = []resource{
|
||||
"/static/css/bootstrap.min.css",
|
||||
"/static/js/jquery-2.1.1.js",
|
||||
"/static/favicon.ico",
|
||||
}
|
||||
|
||||
// if bindata's values matches with the assets/... contents
|
||||
// and secondly if the HandleDir had successfully registered
|
||||
// the routes and gave the correct response.
|
||||
func TestEmbeddingGzipFilesIntoApp(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
// remove the embedded static favicon for !windows,
|
||||
// it should be built for unix-specific in order to be work
|
||||
urls = urls[0 : len(urls)-1]
|
||||
}
|
||||
|
||||
for i, u := range urls {
|
||||
url := u.String()
|
||||
rawContents := u.loadFromBase("./assets")
|
||||
|
||||
response := e.GET(url).Expect()
|
||||
response.ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset())
|
||||
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() {
|
||||
reader, err := gzip.NewReader(strings.NewReader(rawBody))
|
||||
defer reader.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("[%d] of '%s': %v", i, url, err)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
reader.WriteTo(buf)
|
||||
if expected, got := rawContents, buf.String(); expected != got {
|
||||
// t.Fatalf("[%d] of '%s': expected body:\n%s but got:\n%s", i, url, expected, got)
|
||||
// let's reduce the output here...
|
||||
// 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:])
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user