mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
Release of version 10.4.0 - x8 faster embedded file server | Star and Read HISTORY.md
Former-commit-id: 4f8b8c95c1b107a9be3b1ef6835ece949a75ceb6
This commit is contained in:
@@ -10,7 +10,8 @@ import (
|
||||
// $ go build
|
||||
// $ ./embedding-files-into-app
|
||||
// "physical" files are not used, you can delete the "assets" folder and run the example.
|
||||
|
||||
//
|
||||
// See `file-server/embedding-gziped-files-into-app` example as well.
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
|
||||
@@ -34,6 +34,7 @@ func (r resource) loadFromBase(dir string) string {
|
||||
}
|
||||
|
||||
result := string(b)
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
result = strings.Replace(result, "\n", "\r\n", -1)
|
||||
}
|
||||
|
||||
7225
_examples/file-server/embedding-gziped-files-into-app/assets/css/bootstrap.min.css
vendored
Normal file
7225
_examples/file-server/embedding-gziped-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 |
9190
_examples/file-server/embedding-gziped-files-into-app/assets/js/jquery-2.1.1.js
vendored
Normal file
9190
_examples/file-server/embedding-gziped-files-into-app/assets/js/jquery-2.1.1.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
// 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 build
|
||||
// $ ./embedding-gziped-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` and `AssetNames,
|
||||
// that means that you can use both `go-bindata` and `bindata` tools,
|
||||
// the `go-bindata` can be used for the view engine's `Binary` method
|
||||
// and the `bindata` with the `StaticEmbeddedGzip` (x8 times faster than the StaticEmbeded with `go-bindata`).
|
||||
app.StaticEmbeddedGzip("/static", "./assets", GzipAsset, GzipAssetNames)
|
||||
|
||||
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.Run(iris.Addr(":8080"))
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
"github.com/klauspost/compress/gzip"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
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 StaticEmbedded 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()
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func() {
|
||||
reader, err := gzip.NewReader(bytes.NewBuffer(response.Content))
|
||||
defer reader.Close()
|
||||
if err != nil {
|
||||
t.Fatalf("[%d] of '%s': %v", i, url, err)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
reader.WriteTo(buf)
|
||||
if rawContents != buf.String() {
|
||||
t.Fatalf("[%d] of '%s': expected body:\n%s but got:\n%s", i, url, rawContents, buf.String())
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func newApp() *iris.Application {
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
assetHandler := app.StaticEmbeddedHandler("./public", Asset, AssetNames)
|
||||
assetHandler := iris.StaticEmbeddedHandler("./public", Asset, AssetNames, false) // keep that false if you use the `go-bindata` tool.
|
||||
// as an alternative of SPA you can take a look at the /routing/dynamic-path/root-wildcard
|
||||
// example too
|
||||
// or
|
||||
|
||||
Reference in New Issue
Block a user