From 3d8e6a03174c90a8eabafcd9941ae1ceb6578739 Mon Sep 17 00:00:00 2001 From: TangDandan Date: Wed, 13 Nov 2019 15:55:47 +0800 Subject: [PATCH 1/2] Modify core/router/api_builder.go When I use file-server like this ``` v1 := app.Party("/aa/bb") v1.HandleDir(/static, "./assets", iris.DirOptions{ IndexName: "/index.html", Gzip: false, ShowList: false, }) ``` and I request `http://localhost:8080/aa/bb/static` or `http://localhost:8080/aa/bb/cc/static/index.html`, it will be 404 NOT FOUND. Because the modified line will create a route `/aa/bb/aa/bb/static` which expected `/aa/bb/static` Thanks for your consideration. Former-commit-id: cb680a5f8c103e8c14986e1b64505f7faff6326d --- core/router/api_builder.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/router/api_builder.go b/core/router/api_builder.go index 0e80f36e..c90b36d1 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -485,7 +485,8 @@ func (api *APIBuilder) HandleDir(requestPath, directory string, opts ...DirOptio continue } - routes = append(routes, api.createRoutes([]string{http.MethodGet}, s.RequestPath, h)...) + requestPath := s.RequestPath[strings.Index(s.RequestPath, api.relativePath)+len(api.relativePath):] + routes = append(routes, api.createRoutes([]string{http.MethodGet}, requestPath, h)...) getRoute.StaticSites = append(getRoute.StaticSites, s) } From 75dd5b759f0eb256ec0af7a35a1eb9cb125a1709 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 13 Nov 2019 19:14:56 +0200 Subject: [PATCH 2/2] _examples/file-server update to test a party of file server Former-commit-id: 2137bd00d256d57058bedd3b4a5868427cbdc64a --- _examples/file-server/basic/main.go | 13 +++++++------ _examples/file-server/basic/main_test.go | 4 +++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/_examples/file-server/basic/main.go b/_examples/file-server/basic/main.go index 94239ccd..e15ceec6 100644 --- a/_examples/file-server/basic/main.go +++ b/_examples/file-server/basic/main.go @@ -15,7 +15,8 @@ func newApp() *iris.Application { // app.HandleDir("/css", "./assets/css") // app.HandleDir("/js", "./assets/js") - app.HandleDir("/static", "./assets", iris.DirOptions{ + v1 := app.Party("/v1") + v1.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 @@ -33,15 +34,15 @@ func newApp() *iris.Application { }) // You can also register any index handler manually, order of registration does not matter: - // app.Get("/static", [...custom middleware...], func(ctx iris.Context) { + // v1.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 + // http://localhost:8080/v1/static + // http://localhost:8080/v1/static/css/main.css + // http://localhost:8080/v1/static/js/jquery-2.1.1.js + // http://localhost:8080/v1/static/favicon.ico return app } diff --git a/_examples/file-server/basic/main_test.go b/_examples/file-server/basic/main_test.go index 04ab1484..d297c67a 100644 --- a/_examples/file-server/basic/main_test.go +++ b/_examples/file-server/basic/main_test.go @@ -9,6 +9,8 @@ import ( "github.com/kataras/iris/v12/httptest" ) +const prefixURL = "/v1" + type resource string func (r resource) contentType() string { @@ -85,7 +87,7 @@ func TestFileServerBasic(t *testing.T) { url := u.String() contents := u.loadFromBase("./assets") - e.GET(url).Expect(). + e.GET(prefixURL+url).Expect(). Status(httptest.StatusOK). ContentType(u.contentType(), app.ConfigurationReadOnly().GetCharset()). Body().Equal(contents)