Add error message when reverse routing using gorillamux happens before boot

Former-commit-id: 96dd812a16338824474ba1be0db18c486b2ebc0d
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-02-18 08:18:09 +02:00
parent e4079e4c67
commit 9597fab37c
4 changed files with 60 additions and 35 deletions

View File

@ -43,9 +43,18 @@ func main() {
games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/demote", h) games.Post("/{gameID:[0-9]+}/clans/{clanPublicID:[0-9]+}/memberships/demote", h)
} }
app.Get("/anything/{anythingparameter:.*}", func(ctx *iris.Context) { myroute := app.Get("/anything/{anythingparameter:.*}", func(ctx *iris.Context) {
s := ctx.Param("anythingparameter") s := ctx.Param("anythingparameter")
ctx.Writef("The path after /anything is: %s", s) ctx.Writef("The path after /anything is: %s", s)
}) // .ChangeName("myroute")
app.Get("/reverse_myroute", func(ctx *iris.Context) {
// reverse routing snippet using templates:
// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux)
// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter)
myrouteRequestPath := app.Path(myroute.Name(), "anythingparameter", "something/here")
ctx.Writef("Should be '/anything/something/here': %s", myrouteRequestPath)
}) })
p := app.Party("mysubdomain.") p := app.Party("mysubdomain.")

View File

@ -64,9 +64,15 @@ func New() iris.Policies {
// {{ url "providerLink" "provider" "facebook"}} // {{ url "providerLink" "provider" "facebook"}}
// for a path: "/auth/{provider}" with name 'providerLink' // for a path: "/auth/{provider}" with name 'providerLink'
URLPath: func(r iris.RouteInfo, args ...string) string { URLPath: func(r iris.RouteInfo, args ...string) string {
if router == nil {
logger(iris.ProdMode, "gorillamux' reverse routing 'URLPath' should be called after Boot/Listen/Serve")
return ""
}
if r == nil { if r == nil {
return "" return ""
} }
if gr := router.Get(r.Name()); gr != nil { if gr := router.Get(r.Name()); gr != nil {
u, err := gr.URLPath(args...) u, err := gr.URLPath(args...)
if err != nil { if err != nil {

View File

@ -17,6 +17,6 @@ func TestRouteURLPath(t *testing.T) {
expected := "/profile/42/iris-go/something" expected := "/profile/42/iris-go/something"
if got := app.Path("profile", "user_id", 42, "ref", "iris-go", "anything", "something"); got != expected { if got := app.Path("profile", "user_id", 42, "ref", "iris-go", "anything", "something"); got != expected {
t.Fatalf("httprouter's reverse routing 'URLPath' error: expected %s but got %s", expected, got) t.Fatalf("gorillamux' reverse routing 'URLPath' error: expected %s but got %s", expected, got)
} }
} }

76
iris.go
View File

@ -730,7 +730,12 @@ func (s *Framework) Cache(bodyHandler HandlerFunc, expiration time.Duration) Han
} }
// Path used to check arguments with the route's named parameters and return the correct url // Path used to check arguments with the route's named parameters and return the correct url
// if parse failed returns empty string // if parse failed returns empty string.
// Used for reverse routing, depends on router adaptor.
//
// Examples:
// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux)
// https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter)
func (s *Framework) Path(routeName string, args ...interface{}) string { func (s *Framework) Path(routeName string, args ...interface{}) string {
r := s.Router.Routes().Lookup(routeName) r := s.Router.Routes().Lookup(routeName)
if r == nil { if r == nil {
@ -768,39 +773,13 @@ func (s *Framework) Path(routeName string, args ...interface{}) string {
return s.policies.RouterReversionPolicy.URLPath(r, argsString...) return s.policies.RouterReversionPolicy.URLPath(r, argsString...)
} }
// DecodeQuery returns the uri parameter as url (string) // URL returns the subdomain + host + Path(...optional named parameters if route is dynamic)
// useful when you want to pass something to a database and be valid to retrieve it via context.Param // returns an empty string if parse is failed.
// use it only for special cases, when the default behavior doesn't suits you. // Used for reverse routing, depends on router adaptor.
// //
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm // Examples:
// it uses just the url.QueryUnescape // https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_3 (gorillamux)
func DecodeQuery(path string) string { // https://github.com/kataras/iris/tree/v6/adaptors/view/_examples/template_html_4 (httprouter)
if path == "" {
return ""
}
encodedPath, err := url.QueryUnescape(path)
if err != nil {
return path
}
return encodedPath
}
// DecodeURL returns the decoded uri
// useful when you want to pass something to a database and be valid to retrieve it via context.Param
// use it only for special cases, when the default behavior doesn't suits you.
//
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
// it uses just the url.Parse
func DecodeURL(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return uri
}
return u.String()
}
// URL returns the subdomain+ host + Path(...optional named parameters if route is dynamic)
// returns an empty string if parse is failed
func (s *Framework) URL(routeName string, args ...interface{}) (url string) { func (s *Framework) URL(routeName string, args ...interface{}) (url string) {
r := s.Router.Routes().Lookup(routeName) r := s.Router.Routes().Lookup(routeName)
if r == nil { if r == nil {
@ -833,6 +812,37 @@ func (s *Framework) URL(routeName string, args ...interface{}) (url string) {
return return
} }
// DecodeQuery returns the uri parameter as url (string)
// useful when you want to pass something to a database and be valid to retrieve it via context.Param
// use it only for special cases, when the default behavior doesn't suits you.
//
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
// it uses just the url.QueryUnescape
func DecodeQuery(path string) string {
if path == "" {
return ""
}
encodedPath, err := url.QueryUnescape(path)
if err != nil {
return path
}
return encodedPath
}
// DecodeURL returns the decoded uri
// useful when you want to pass something to a database and be valid to retrieve it via context.Param
// use it only for special cases, when the default behavior doesn't suits you.
//
// http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
// it uses just the url.Parse
func DecodeURL(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return uri
}
return u.String()
}
var errTemplateRendererIsMissing = errors.New( var errTemplateRendererIsMissing = errors.New(
` `
manually call of Render for a template: '%s' without specified RenderPolicy! manually call of Render for a template: '%s' without specified RenderPolicy!