From 4de98254fd3c7e4b586b89d74f3e3f7ee115589b Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Wed, 22 Feb 2017 16:38:55 +0200 Subject: [PATCH] (silly) Fix https://github.com/kataras/iris/issues/633 Former-commit-id: 0359fbf7dd3ff151db7b0575510e611bed683d0e --- HISTORY.md | 1 + adaptors/gorillamux/gorillamux.go | 12 ++++++++++-- adaptors/httprouter/httprouter.go | 10 +++++++++- policy.go | 8 ++++++++ router.go | 11 +++++------ 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b0128233..e5a814da 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -34,6 +34,7 @@ Fixes: - Fix and improve the cloud-editor `alm/alm-tools` plugin(now adaptor) - Fix gorillamux serve static files (custom routers are supported with a workaround, not a complete solution as they are now) - Fix `iris run main.go` app reload while user saved the file from gogland +- Fix [StaticEmbedded doesn't works on root "/"](https://github.com/kataras/iris/issues/633) Changes: diff --git a/adaptors/gorillamux/gorillamux.go b/adaptors/gorillamux/gorillamux.go index 162ea0f4..531e9f81 100644 --- a/adaptors/gorillamux/gorillamux.go +++ b/adaptors/gorillamux/gorillamux.go @@ -62,8 +62,16 @@ func New() iris.Policies { RouterReversionPolicy: iris.RouterReversionPolicy{ // path normalization done on iris' side StaticPath: staticPath, - WildcardPath: func(requestPath string, paramName string) string { - return requestPath + "/{" + paramName + ":.*}" + WildcardPath: func(path string, paramName string) string { + // {param:.*} + wildcardPart := "{" + paramName + ":.*}" + + if path[len(path)-1] != '/' { + // if not ending with slash then prepend the slash to the wildcard path part + wildcardPart = "/" + wildcardPart + } + // finally return the path given + the wildcard path part + return path + wildcardPart }, // Note: on gorilla mux the {{ url }} and {{ path}} should give the key and the value, not only the values by order. // {{ url "nameOfTheRoute" "parameterName" "parameterValue"}}. diff --git a/adaptors/httprouter/httprouter.go b/adaptors/httprouter/httprouter.go index c67c3997..de3f347f 100644 --- a/adaptors/httprouter/httprouter.go +++ b/adaptors/httprouter/httprouter.go @@ -536,7 +536,15 @@ func New() iris.Policies { return path }, WildcardPath: func(path string, paramName string) string { - return path + slash + matchEverythingString + paramName + // *param + wildcardPart := matchEverythingString + paramName + + if path[len(path)-1] != slashByte { + // if not ending with slash then prepend the slash to the wildcard path part + wildcardPart = slash + wildcardPart + } + // finally return the path given + the wildcard path part + return path + wildcardPart }, // path = "/api/users/:id", args = ["42"] // return "/api/users/42" diff --git a/policy.go b/policy.go index ab899380..20fc80ff 100644 --- a/policy.go +++ b/policy.go @@ -285,11 +285,19 @@ type ( ) func normalizePath(path string) string { + // some users can't understand the difference between + // request path and operating system's directory path + // they think that "./" is the index, that's wrong, "/" is the index + // so fix that here... + if path[0] == '.' { + path = path[1:] + } path = strings.Replace(path, "//", "/", -1) if len(path) > 1 && strings.IndexByte(path, '/') == len(path)-1 { // if it's not "/" and ending with slash remove that slash path = path[0 : len(path)-2] } + return path } diff --git a/router.go b/router.go index a2774e47..a7c8d78a 100644 --- a/router.go +++ b/router.go @@ -487,7 +487,9 @@ func (router *Router) StaticContent(reqPath string, cType string, content []byte // example: https://github.com/iris-contrib/examples/tree/master/static_files_embedded func (router *Router) StaticEmbedded(requestPath string, vdir string, assetFn func(name string) ([]byte, error), namesFn func() []string) RouteInfo { paramName := "path" - requestPath = router.Context.Framework().policies.RouterReversionPolicy.WildcardPath(requestPath, paramName) + s := router.Context.Framework() + + requestPath = s.policies.RouterReversionPolicy.WildcardPath(requestPath, paramName) if len(vdir) > 0 { if vdir[0] == '.' { // first check for .wrong @@ -512,20 +514,17 @@ func (router *Router) StaticEmbedded(requestPath string, vdir string, assetFn fu path = strings.Replace(path, "./", "/", -1) // replace ./assets/favicon.ico to /assets/favicon.ico in order to be ready for compare with the reqPath later path = path[len(vdir):] // set it as the its 'relative' ( we should re-setted it when assetFn will be used) names = append(names, path) - } + if len(names) == 0 { // we don't start the server yet, so: - panic("iris.StaticEmbedded: Unable to locate any embedded files located to the (virtual) directory: " + vdir) + s.Log(ProdMode, "error on StaticEmbedded: unable to locate any embedded files located to the (virtual) directory: "+vdir) } modtime := time.Now() h := func(ctx *Context) { - reqPath := ctx.Param(paramName) - for _, path := range names { - if path != reqPath { continue }