Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Feature/Addition Host domain #2185

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion route_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (ri *RouteInfo) BuildPathHelper() RouteHelperFunc {
return "", fmt.Errorf("missing parameters for %v: %s", cRoute.Path, err)
}

result := url.Path
result := url.String()
result = addExtraParamsTo(result, opts)

return template.HTML(result), nil
Expand Down
23 changes: 23 additions & 0 deletions route_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ func (a *App) Mount(p string, h http.Handler) {
a.ANY(path, WrapHandler(http.StripPrefix(prefix, h)))
}

// Host creates a new "*App" group that matches the domain passed.
// This is useful for creating groups of end-points for different domains.
/*
a.Host("www.example.com")
a.Host("{subdomain}.example.com")
a.Host("{subdomain:[a-z]+}.example.com")
*/
func (a *App) Host(h string) *App {
g := New(a.Options)

g.router = a.router.Host(h).Subrouter()
g.RouteNamer = a.RouteNamer
g.Middleware = a.Middleware.clone()
g.ErrorHandlers = a.ErrorHandlers
g.root = a

if a.root != nil {
g.root = a.root
}

return g
}

// ServeFiles maps an path to a directory on disk to serve static files.
// Useful for JavaScript, images, CSS, etc...
/*
Expand Down
57 changes: 57 additions & 0 deletions route_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,60 @@ func Test_App_Routes_Resource(t *testing.T) {
}
}
}

func Test_App_Host(t *testing.T) {
r := require.New(t)

a1 := New(Options{})
r.Nil(a1.root)

h1 := a1.Host("www.example.com")
h1.GET("/foo", voidHandler)

routes := h1.Routes()
r.Len(routes, 1)

route := routes[0]
r.Equal("GET", route.Method)
r.Equal("/foo/", route.Path)
r.NotZero(route.HandlerName)

// With Regular Expressions

a2 := New(Options{})
r.Nil(a1.root)

h2 := a2.Host("{subdomain}.example.com")
h2.GET("/foo", voidHandler)
h2.GET("/foo/{id}", voidHandler).Name("fooID")

rh := h2.RouteHelpers()

routes = h2.Routes()
r.Len(routes, 2)

r.Equal("GET", routes[0].Method)
r.Equal("/foo/", routes[0].Path)
r.NotZero(routes[0].HandlerName)

r.Equal("GET", routes[1].Method)
r.Equal("/foo/{id}/", routes[1].Path)
r.NotZero(routes[1].HandlerName)

f, ok := rh["fooPath"]
r.True(ok)
x, err := f(map[string]interface{}{
"subdomain": "test",
})
r.NoError(err)
r.Equal("http://test.example.com/foo/", string(x))

f, ok = rh["fooIDPath"]
r.True(ok)
x, err = f(map[string]interface{}{
"subdomain": "test",
"id": 1,
})
r.NoError(err)
r.Equal("http://test.example.com/foo/1/", string(x))
}