mirror of
https://github.com/kataras/iris.git
synced 2026-01-19 09:56:02 +00:00
create the new FileServer and HandleDir, deprecate the rest APIBuilder/Party static methods and more
relative: https://github.com/kataras/iris/issues/1283 and removing pongo2 from vendor: https://github.com/kataras/iris/issues/1284 Former-commit-id: 3ec57b349f99faca2b8e36d9f7252db0b6ea080d
This commit is contained in:
@@ -0,0 +1 @@
|
||||
just a text.
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Hello App2App3 index</h1>
|
||||
1
_examples/file-server/basic/assets/app2/index.html
Normal file
1
_examples/file-server/basic/assets/app2/index.html
Normal file
@@ -0,0 +1 @@
|
||||
<h1>Hello App2 index</h1>
|
||||
@@ -4,38 +4,48 @@ import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
|
||||
app.Favicon("./assets/favicon.ico")
|
||||
|
||||
// enable gzip, optionally:
|
||||
// if used before the `StaticXXX` handlers then
|
||||
// the content byte range feature is gone.
|
||||
// recommend: turn off for large files especially
|
||||
// when server has low memory,
|
||||
// turn on for medium-sized files
|
||||
// or for large-sized files if they are zipped already,
|
||||
// i.e "zippedDir/file.gz"
|
||||
//
|
||||
// app.Use(iris.Gzip)
|
||||
|
||||
// first parameter is the request path
|
||||
// second is the system directory
|
||||
//
|
||||
// app.StaticWeb("/css", "./assets/css")
|
||||
// app.StaticWeb("/js", "./assets/js")
|
||||
//
|
||||
app.StaticWeb("/static", "./assets")
|
||||
// app.HandleDir("/css", "./assets/css")
|
||||
// app.HandleDir("/js", "./assets/js")
|
||||
|
||||
app.HandleDir("/static", "./assets", iris.DirOptions{
|
||||
// Defaults to "/index.html", if request path is ending with **/*/$IndexName
|
||||
// then it redirects to **/*(/) which another handler is handling it,
|
||||
// that another handler, called index handler, is auto-registered by the framework
|
||||
// if end developer does not managed to handle it by hand.
|
||||
IndexName: "/index.html",
|
||||
// When files should served under compression.
|
||||
Gzip: false,
|
||||
// List the files inside the current requested directory if `IndexName` not found.
|
||||
ShowList: false,
|
||||
// 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).
|
||||
// DirList: func(ctx context.Context, dirName string, dir http.File) error { ... }
|
||||
//
|
||||
// Optional validator that loops through each requested resource.
|
||||
// AssetValidator: func(ctx iris.Context, name string) bool { ... }
|
||||
})
|
||||
|
||||
// You can also register any index handler manually, order of registration does not matter:
|
||||
// app.Get("/static", [...custom middleware...], func(ctx iris.Context) {
|
||||
// [...custom code...]
|
||||
// ctx.ServeFile("./assets/index.html", false)
|
||||
// })
|
||||
|
||||
// http://localhost:8080/static
|
||||
// http://localhost:8080/static/css/main.css
|
||||
// http://localhost:8080/static/js/jquery-2.1.1.js
|
||||
// http://localhost:8080/static/favicon.ico
|
||||
app.Run(iris.Addr(":8080"))
|
||||
|
||||
// Note:
|
||||
// Routing doesn't allows something .StaticWeb("/", "./assets")
|
||||
//
|
||||
// To see how you can wrap the router in order to achieve
|
||||
// wildcard on root path, see "single-page-application".
|
||||
return app
|
||||
}
|
||||
|
||||
func main() {
|
||||
app := newApp()
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
93
_examples/file-server/basic/main_test.go
Normal file
93
_examples/file-server/basic/main_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/httptest"
|
||||
)
|
||||
|
||||
type resource string
|
||||
|
||||
func (r resource) contentType() string {
|
||||
switch filepath.Ext(r.String()) {
|
||||
case ".js":
|
||||
return "application/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")
|
||||
if filepath.Ext(filename) == "" {
|
||||
// root /.
|
||||
filename = filename + "/index.html"
|
||||
}
|
||||
|
||||
fullpath := filepath.Join(dir, filename)
|
||||
|
||||
b, err := ioutil.ReadFile(fullpath)
|
||||
if err != nil {
|
||||
panic(fullpath + " failed with error: " + err.Error())
|
||||
}
|
||||
|
||||
result := string(b)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func TestFileServerBasic(t *testing.T) {
|
||||
var urls = []resource{
|
||||
"/static/css/main.css",
|
||||
"/static/js/jquery-2.1.1.js",
|
||||
"/static/favicon.ico",
|
||||
"/static/app2",
|
||||
"/static/app2/app2app3",
|
||||
"/static",
|
||||
}
|
||||
|
||||
app := newApp()
|
||||
// route := app.GetRouteReadOnly("GET/{file:path}")
|
||||
// if route == nil {
|
||||
// app.Logger().Fatalf("expected a route to serve files")
|
||||
// }
|
||||
|
||||
// if expected, got := "./assets", route.StaticDir(); expected != got {
|
||||
// app.Logger().Fatalf("expected route's static directory to be: '%s' but got: '%s'", expected, got)
|
||||
// }
|
||||
|
||||
// if !route.StaticDirContainsIndex() {
|
||||
// app.Logger().Fatalf("epxected ./assets to contain an %s file", "/index.html")
|
||||
// }
|
||||
|
||||
e := httptest.New(t, app)
|
||||
for _, u := range urls {
|
||||
url := u.String()
|
||||
contents := u.loadFromBase("./assets")
|
||||
|
||||
e.GET(url).Expect().
|
||||
Status(httptest.StatusOK).
|
||||
ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset()).
|
||||
Body().Equal(contents)
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,14 @@ import (
|
||||
// See `file-server/embedding-gziped-files-into-app` example as well.
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.Logger().SetLevel("debug")
|
||||
|
||||
app.StaticEmbedded("/static", "./assets", Asset, AssetNames)
|
||||
app.HandleDir("/static", "./assets", iris.DirOptions{
|
||||
Asset: Asset,
|
||||
AssetInfo: AssetInfo,
|
||||
AssetNames: AssetNames,
|
||||
ShowList: true,
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -66,12 +66,21 @@ var urls = []resource{
|
||||
}
|
||||
|
||||
// if bindata's values matches with the assets/... contents
|
||||
// and secondly if the StaticEmbedded had successfully registered
|
||||
// and secondly if the HandleDir had successfully registered
|
||||
// the routes and gave the correct response.
|
||||
func TestEmbeddingFilesIntoApp(t *testing.T) {
|
||||
app := newApp()
|
||||
e := httptest.New(t, app)
|
||||
|
||||
route := app.GetRouteReadOnly("GET/static/{file:path}")
|
||||
if route == nil {
|
||||
t.Fatalf("expected a route to serve embedded files")
|
||||
}
|
||||
|
||||
if len(route.StaticSites()) > 0 {
|
||||
t.Fatalf("not expected a static site, the ./assets directory or its subdirectories do not contain any index.html")
|
||||
}
|
||||
|
||||
if runtime.GOOS != "windows" {
|
||||
// remove the embedded static favicon for !windows,
|
||||
// it should be built for unix-specific in order to be work
|
||||
|
||||
@@ -16,12 +16,13 @@ import (
|
||||
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)
|
||||
|
||||
// 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.
|
||||
app.HandleDir("/static", "./assets", iris.DirOptions{
|
||||
Asset: GzipAsset,
|
||||
AssetInfo: GzipAssetInfo,
|
||||
AssetNames: GzipAssetNames,
|
||||
})
|
||||
return app
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ var urls = []resource{
|
||||
}
|
||||
|
||||
// if bindata's values matches with the assets/... contents
|
||||
// and secondly if the StaticEmbedded had successfully registered
|
||||
// and secondly if the HandleDir had successfully registered
|
||||
// the routes and gave the correct response.
|
||||
func TestEmbeddingGzipFilesIntoApp(t *testing.T) {
|
||||
app := newApp()
|
||||
|
||||
@@ -20,15 +20,7 @@ func newApp() *iris.Application {
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
// or just serve index.html as it is:
|
||||
// app.Get("/{f:path}", func(ctx iris.Context) {
|
||||
// ctx.ServeFile("index.html", false)
|
||||
// })
|
||||
|
||||
assetHandler := app.StaticHandler("./public", false, false)
|
||||
// as an alternative of SPA you can take a look at the /routing/dynamic-path/root-wildcard
|
||||
// example too
|
||||
app.SPA(assetHandler)
|
||||
app.HandleDir("/", "./public")
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@@ -9,11 +9,21 @@ import "github.com/kataras/iris"
|
||||
|
||||
func newApp() *iris.Application {
|
||||
app := iris.New()
|
||||
app.OnErrorCode(404, func(ctx iris.Context) {
|
||||
app.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
|
||||
ctx.Writef("404 not found here")
|
||||
})
|
||||
|
||||
app.StaticEmbedded("/", "./public", Asset, AssetNames)
|
||||
app.HandleDir("/", "./public", iris.DirOptions{
|
||||
Asset: Asset,
|
||||
AssetInfo: AssetInfo,
|
||||
AssetNames: AssetNames,
|
||||
// IndexName: "index.html", // default.
|
||||
// If you want to show a list of embedded files when inside a directory without an index file:
|
||||
// ShowList: true,
|
||||
// DirList: func(ctx iris.Context, dirName string, f http.File) error {
|
||||
// // [Optional, custom code to show the html list].
|
||||
// }
|
||||
})
|
||||
|
||||
// Note:
|
||||
// if you want a dynamic index page then see the file-server/embedded-single-page-application
|
||||
|
||||
@@ -22,16 +22,11 @@ func newApp() *iris.Application {
|
||||
ctx.View("index.html")
|
||||
})
|
||||
|
||||
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
|
||||
// app.StaticEmbedded if you don't want to redirect on index.html and simple serve your SPA app (recommended).
|
||||
|
||||
// public/index.html is a dynamic view, it's handlded by root,
|
||||
// and we don't want to be visible as a raw data, so we will
|
||||
// the return value of `app.SPA` to modify the `IndexNames` by;
|
||||
app.SPA(assetHandler).AddIndexName("index.html")
|
||||
app.HandleDir("/", "./public", iris.DirOptions{
|
||||
Asset: Asset,
|
||||
AssetInfo: AssetInfo,
|
||||
AssetNames: AssetNames,
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -45,11 +40,3 @@ func main() {
|
||||
// http://localhost:8080/css/main.css
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
// Note that app.Use/UseGlobal/Done will be executed
|
||||
// only to the registered routes like our index (app.Get("/", ..)).
|
||||
// The file server is clean, but you can still add middleware to that by wrapping its "assetHandler".
|
||||
//
|
||||
// With this method, unlike StaticWeb("/" , "./public") which is not working by-design anymore,
|
||||
// all custom http errors and all routes are working fine with a file server that is registered
|
||||
// to the root path of the server.
|
||||
|
||||
Reference in New Issue
Block a user