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

Commit

Permalink
simplified, renamed the multi-homing function (Host -> VirtualHost)
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 authored and paganotoni committed Apr 19, 2022
1 parent f3c11a1 commit 8176217
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
51 changes: 24 additions & 27 deletions route_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,6 @@ 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 {
// TODO: move this function to app.go or home.go eventually.
// in the end, it should return *Home.
g := New(a.Options)

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

g.app = a.app // will replace g.root
g.root = g.app // will be deprecated

// to be replaced with child Homes. currently, only used in grifts.
a.children = append(a.children, g)

return g
}

// ServeFiles maps an path to a directory on disk to serve static files.
// Useful for JavaScript, images, CSS, etc...
/*
Expand Down Expand Up @@ -291,6 +264,30 @@ func (a *App) Group(groupPath string) *App {
return g
}

// VirtualHost creates a new `*App` that inherits from it's parent `*App`.
// All pre-configured things on the parent App such as middlewares will be
// applied, and can be modified only for this child App.
//
// This is a multi-homing feature similar to the `VirtualHost` in Apache
// or multiple `server`s in nginx. One important different behavior is that
// there is no concept of the `default` host in buffalo (at least for now)
// and the routing decision will be made with the "first match" manner.
// (e.g. if you have already set the route for '/' for the root App before
// setting up a virualhost, the route of the root App will be picked up
// even if the client makes a request to the specified domain.)
/*
a.VirtualHost("www.example.com")
a.VirtualHost("{subdomain}.example.com")
a.VirtualHost("{subdomain:[a-z]+}.example.com")
*/
func (a *App) VirtualHost(h string) *App {
g := a.Group("/")
g.host = h
g.router = a.router.Host(h).Subrouter()

return g
}

// RouteHelpers returns a map of BuildPathHelper() for each route available in the app.
func (a *App) RouteHelpers() map[string]RouteHelperFunc {
rh := map[string]RouteHelperFunc{}
Expand Down
6 changes: 3 additions & 3 deletions route_mappings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ func Test_App_Routes_Resource(t *testing.T) {
}
}

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

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

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

routes := h1.Routes()
Expand All @@ -177,7 +177,7 @@ func Test_App_Host(t *testing.T) {
a2 := New(Options{})
r.Nil(a1.root)

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

Expand Down

0 comments on commit 8176217

Please sign in to comment.