Skip to content

Commit

Permalink
#213 bug fix route url generation for single domain use case (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Aug 28, 2018
2 parents eeb7f0f + 100a0fa commit e2923c0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
<p align="center">Visit aah's official website https://aahframework.org to learn more</p>
</p>
<p align="center">
<p align="center"><a href="https://travis-ci.org/go-aah/aah"><img src="https://travis-ci.org/go-aah/aah.svg?branch=master" alt="Build Status"></a> <a href="https://codecov.io/gh/go-aah/aah/branch/master"><img src="https://codecov.io/gh/go-aah/aah/branch/master/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/aahframework.org/aah.v0"><img src="https://goreportcard.com/badge/aahframework.org/aah.v0" alt="Go Report Card"></a> <a href="https://github.com/go-aah/aah/releases/latest"><img src="https://img.shields.io/badge/version-0.11.2-blue.svg" alt="Release Version"></a> <a href="https://godoc.org/aahframework.org/aah.v0"><img src="https://godoc.org/aahframework.org/aah.v0?status.svg" alt="Godoc"></a> <a href="https://hub.docker.com/r/aahframework/aah/"><img src="https://img.shields.io/docker/pulls/aahframework/aah.svg" alt="Docker Pulls"></a> <a href="https://twitter.com/aahframework"><img src="https://img.shields.io/badge/twitter-@aahframework-55acee.svg" alt="Twitter @aahframework"></a></p>
<p align="center"><a href="https://travis-ci.org/go-aah/aah"><img src="https://travis-ci.org/go-aah/aah.svg?branch=master" alt="Build Status"></a> <a href="https://codecov.io/gh/go-aah/aah/branch/master"><img src="https://codecov.io/gh/go-aah/aah/branch/master/graph/badge.svg" alt="Code Coverage"></a> <a href="https://goreportcard.com/report/aahframework.org/aah.v0"><img src="https://goreportcard.com/badge/aahframework.org/aah.v0" alt="Go Report Card"></a> <a href="https://github.com/go-aah/aah/releases/latest"><img src="https://img.shields.io/badge/version-0.11.4-blue.svg" alt="Release Version"></a> <a href="https://godoc.org/aahframework.org/aah.v0"><img src="https://godoc.org/aahframework.org/aah.v0?status.svg" alt="Godoc"></a> <a href="https://hub.docker.com/r/aahframework/aah/"><img src="https://img.shields.io/docker/pulls/aahframework/aah.svg" alt="Docker Pulls"></a> <a href="https://twitter.com/aahframework"><img src="https://img.shields.io/badge/twitter-@aahframework-55acee.svg" alt="Twitter @aahframework"></a></p>
</p>

### News

* `v0.11.2` [released](https://docs.aahframework.org/release-notes.html) and tagged on Jul 27, 2018.
* `v0.11.4` [released](https://docs.aahframework.org/release-notes.html) and tagged on Aug 27, 2018.

### Stargazers over time

Expand Down
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func (ctx *Context) AddViewArg(key string, value interface{}) *Context {
// See `router.Domain.RouteURL` for more information.
func (ctx *Context) RouteURL(routeName string, args ...interface{}) string {
domain, rn := ctx.a.findRouteURLDomain(ctx.Req.Host, routeName)
return createRouteURL(ctx.Log(), domain, rn, nil, args...)
return createRouteURL(ctx.Log(), ctx.Req.Host, domain, rn, nil, args...)
}

// RouteURLNamedArgs method returns the URL for given route name and key-value paris.
// See `router.Domain.RouteURLNamedArgs` for more information.
func (ctx *Context) RouteURLNamedArgs(routeName string, args map[string]interface{}) string {
domain, rn := ctx.a.findRouteURLDomain(ctx.Req.Host, routeName)
return createRouteURL(ctx.Log(), domain, rn, args)
return createRouteURL(ctx.Log(), ctx.Req.Host, domain, rn, args)
}

// Msg method returns the i18n value for given key otherwise empty string returned.
Expand Down
12 changes: 8 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ func getRouteNameAndAnchorLink(routeName string) (string, string) {
return routeName, anchorLink
}

func composeRouteURL(domain *router.Domain, routePath, anchorLink string) string {
func composeRouteURL(host string, domain *router.Domain, routePath, anchorLink string) string {
if domain.Host == "localhost" {
return appendAnchorLink("//"+host+routePath, anchorLink)
}

if len(domain.Port) == 0 {
routePath = fmt.Sprintf("//%s%s", domain.Host, routePath)
} else {
Expand Down Expand Up @@ -257,9 +261,9 @@ func (a *app) findRouteURLDomain(host, routeName string) (*router.Domain, string
return a.Router().RootDomain(), routeName
}

func createRouteURL(l log.Loggerer, domain *router.Domain, routeName string, margs map[string]interface{}, args ...interface{}) string {
func createRouteURL(l log.Loggerer, host string, domain *router.Domain, routeName string, margs map[string]interface{}, args ...interface{}) string {
if routeName == "host" {
return composeRouteURL(domain, "", "")
return composeRouteURL(host, domain, "", "")
}

routeName, anchorLink := getRouteNameAndAnchorLink(routeName)
Expand All @@ -271,7 +275,7 @@ func createRouteURL(l log.Loggerer, domain *router.Domain, routeName string, mar
}

// URL escapes
rURL, err := url.Parse(composeRouteURL(domain, routePath, anchorLink))
rURL, err := url.Parse(composeRouteURL(host, domain, routePath, anchorLink))
if err != nil {
l.Error(err)
return ""
Expand Down
2 changes: 1 addition & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestRouterTemplateFuncs(t *testing.T) {

func TestRouterMisc(t *testing.T) {
domain := &router.Domain{Host: "localhost"}
result := composeRouteURL(domain, "/path", "my-head")
result := composeRouteURL("localhost", domain, "/path", "my-head")
assert.Equal(t, "//localhost/path#my-head", result)
}

Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
package aah

// Version no. of aah framework
const Version = "0.11.3"
const Version = "0.11.4"
10 changes: 6 additions & 4 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,19 @@ func (vm *viewManager) tmplURL(viewArgs map[string]interface{}, args ...interfac
vm.a.Log().Errorf("router: template 'rurl' - route name is empty: %v", args)
return template.URL("#")
}
domain, routeName := vm.a.findRouteURLDomain(viewArgs["Host"].(string), args[0].(string))
host := viewArgs["Host"].(string)
domain, routeName := vm.a.findRouteURLDomain(host, args[0].(string))
/* #nosec */
return template.URL(createRouteURL(vm.a.Log(), domain, routeName, nil, args[1:]...))
return template.URL(createRouteURL(vm.a.Log(), host, domain, routeName, nil, args[1:]...))
}

// tmplURLm method returns reverse URL by given route name and
// map[string]interface{}. Mapped to Go template func.
func (vm *viewManager) tmplURLm(viewArgs map[string]interface{}, routeName string, args map[string]interface{}) template.URL {
domain, rn := vm.a.findRouteURLDomain(viewArgs["Host"].(string), routeName)
host := viewArgs["Host"].(string)
domain, rn := vm.a.findRouteURLDomain(host, routeName)
/* #nosec */
return template.URL(createRouteURL(vm.a.Log(), domain, rn, args))
return template.URL(createRouteURL(vm.a.Log(), host, domain, rn, args))
}

//
Expand Down

0 comments on commit e2923c0

Please sign in to comment.