Skip to content

Commit 6f0a227

Browse files
committed
Built-in capability to run multiple hosts
Signed-off-by: Vishal Rana <[email protected]>
1 parent e53d9c5 commit 6f0a227

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

echo.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type (
7070
middleware []MiddlewareFunc
7171
maxParam *int
7272
router *Router
73+
routers map[string]*Router
7374
notFoundHandler HandlerFunc
7475
pool sync.Pool
7576
Server *http.Server
@@ -308,6 +309,7 @@ func New() (e *Echo) {
308309
return e.NewContext(nil, nil)
309310
}
310311
e.router = NewRouter(e)
312+
e.routers = map[string]*Router{}
311313
return
312314
}
313315

@@ -481,11 +483,10 @@ func (e *Echo) File(path, file string, m ...MiddlewareFunc) *Route {
481483
}, m...)
482484
}
483485

484-
// Add registers a new route for an HTTP method and path with matching handler
485-
// in the router with optional route-level middleware.
486-
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
486+
func (e *Echo) add(host, method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
487487
name := handlerName(handler)
488-
e.router.Add(method, path, func(c Context) error {
488+
router := e.findRouter(host)
489+
router.Add(method, path, func(c Context) error {
489490
h := handler
490491
// Chain middleware
491492
for i := len(middleware) - 1; i >= 0; i-- {
@@ -502,6 +503,20 @@ func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...Middl
502503
return r
503504
}
504505

506+
// Add registers a new route for an HTTP method and path with matching handler
507+
// in the router with optional route-level middleware.
508+
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
509+
return e.add("", method, path, handler, middleware...)
510+
}
511+
512+
// Host creates a new router group for the provided host and optional host-level middleware.
513+
func (e *Echo) Host(name string, m ...MiddlewareFunc) (g *Group) {
514+
e.routers[name] = NewRouter(e)
515+
g = &Group{host: name, echo: e}
516+
g.Use(m...)
517+
return
518+
}
519+
505520
// Group creates a new router group with prefix and optional group-level middleware.
506521
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
507522
g = &Group{prefix: prefix, echo: e}
@@ -574,12 +589,12 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
574589
h := NotFoundHandler
575590

576591
if e.premiddleware == nil {
577-
e.router.Find(r.Method, getPath(r), c)
592+
e.findRouter(r.Host).Find(r.Method, getPath(r), c)
578593
h = c.Handler()
579594
h = applyMiddleware(h, e.middleware...)
580595
} else {
581596
h = func(c Context) error {
582-
e.router.Find(r.Method, getPath(r), c)
597+
e.findRouter(r.Host).Find(r.Method, getPath(r), c)
583598
h := c.Handler()
584599
h = applyMiddleware(h, e.middleware...)
585600
return h(c)
@@ -761,6 +776,15 @@ func getPath(r *http.Request) string {
761776
return path
762777
}
763778

779+
func (e *Echo) findRouter(host string) *Router {
780+
if len(e.routers) > 0 {
781+
if r, ok := e.routers[host]; ok {
782+
return r
783+
}
784+
}
785+
return e.router
786+
}
787+
764788
func handlerName(h HandlerFunc) string {
765789
t := reflect.ValueOf(h).Type()
766790
if t.Kind() == reflect.Func {

group.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type (
1010
// routes that share a common middleware or functionality that should be separate
1111
// from the parent echo instance while still inheriting from it.
1212
Group struct {
13+
host string
1314
prefix string
1415
middleware []MiddlewareFunc
1516
echo *Echo
@@ -117,5 +118,5 @@ func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...Midd
117118
m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
118119
m = append(m, g.middleware...)
119120
m = append(m, middleware...)
120-
return g.echo.Add(method, g.prefix+path, handler, m...)
121+
return g.echo.add(g.host, method, g.prefix+path, handler, m...)
121122
}

router.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,13 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
7979

8080
if i == l {
8181
r.insert(method, path[:i], h, pkind, ppath, pnames)
82-
return
82+
} else {
83+
r.insert(method, path[:i], nil, pkind, "", nil)
8384
}
84-
r.insert(method, path[:i], nil, pkind, "", nil)
8585
} else if path[i] == '*' {
8686
r.insert(method, path[:i], nil, skind, "", nil)
8787
pnames = append(pnames, "*")
8888
r.insert(method, path[:i+1], h, akind, ppath, pnames)
89-
return
9089
}
9190
}
9291

0 commit comments

Comments
 (0)