1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Built-in capability to run multiple hosts

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2019-04-28 22:22:35 -07:00
parent e53d9c516f
commit 6f0a227a5a
3 changed files with 34 additions and 10 deletions

36
echo.go
View File

@ -70,6 +70,7 @@ type (
middleware []MiddlewareFunc middleware []MiddlewareFunc
maxParam *int maxParam *int
router *Router router *Router
routers map[string]*Router
notFoundHandler HandlerFunc notFoundHandler HandlerFunc
pool sync.Pool pool sync.Pool
Server *http.Server Server *http.Server
@ -308,6 +309,7 @@ func New() (e *Echo) {
return e.NewContext(nil, nil) return e.NewContext(nil, nil)
} }
e.router = NewRouter(e) e.router = NewRouter(e)
e.routers = map[string]*Router{}
return return
} }
@ -481,11 +483,10 @@ func (e *Echo) File(path, file string, m ...MiddlewareFunc) *Route {
}, m...) }, m...)
} }
// Add registers a new route for an HTTP method and path with matching handler func (e *Echo) add(host, method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
// in the router with optional route-level middleware.
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
name := handlerName(handler) name := handlerName(handler)
e.router.Add(method, path, func(c Context) error { router := e.findRouter(host)
router.Add(method, path, func(c Context) error {
h := handler h := handler
// Chain middleware // Chain middleware
for i := len(middleware) - 1; i >= 0; i-- { for i := len(middleware) - 1; i >= 0; i-- {
@ -502,6 +503,20 @@ func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...Middl
return r return r
} }
// Add registers a new route for an HTTP method and path with matching handler
// in the router with optional route-level middleware.
func (e *Echo) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
return e.add("", method, path, handler, middleware...)
}
// Host creates a new router group for the provided host and optional host-level middleware.
func (e *Echo) Host(name string, m ...MiddlewareFunc) (g *Group) {
e.routers[name] = NewRouter(e)
g = &Group{host: name, echo: e}
g.Use(m...)
return
}
// Group creates a new router group with prefix and optional group-level middleware. // Group creates a new router group with prefix and optional group-level middleware.
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) { func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
g = &Group{prefix: prefix, echo: e} g = &Group{prefix: prefix, echo: e}
@ -574,12 +589,12 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h := NotFoundHandler h := NotFoundHandler
if e.premiddleware == nil { if e.premiddleware == nil {
e.router.Find(r.Method, getPath(r), c) e.findRouter(r.Host).Find(r.Method, getPath(r), c)
h = c.Handler() h = c.Handler()
h = applyMiddleware(h, e.middleware...) h = applyMiddleware(h, e.middleware...)
} else { } else {
h = func(c Context) error { h = func(c Context) error {
e.router.Find(r.Method, getPath(r), c) e.findRouter(r.Host).Find(r.Method, getPath(r), c)
h := c.Handler() h := c.Handler()
h = applyMiddleware(h, e.middleware...) h = applyMiddleware(h, e.middleware...)
return h(c) return h(c)
@ -761,6 +776,15 @@ func getPath(r *http.Request) string {
return path return path
} }
func (e *Echo) findRouter(host string) *Router {
if len(e.routers) > 0 {
if r, ok := e.routers[host]; ok {
return r
}
}
return e.router
}
func handlerName(h HandlerFunc) string { func handlerName(h HandlerFunc) string {
t := reflect.ValueOf(h).Type() t := reflect.ValueOf(h).Type()
if t.Kind() == reflect.Func { if t.Kind() == reflect.Func {

View File

@ -10,6 +10,7 @@ type (
// routes that share a common middleware or functionality that should be separate // routes that share a common middleware or functionality that should be separate
// from the parent echo instance while still inheriting from it. // from the parent echo instance while still inheriting from it.
Group struct { Group struct {
host string
prefix string prefix string
middleware []MiddlewareFunc middleware []MiddlewareFunc
echo *Echo echo *Echo
@ -117,5 +118,5 @@ func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...Midd
m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware)) m := make([]MiddlewareFunc, 0, len(g.middleware)+len(middleware))
m = append(m, g.middleware...) m = append(m, g.middleware...)
m = append(m, middleware...) m = append(m, middleware...)
return g.echo.Add(method, g.prefix+path, handler, m...) return g.echo.add(g.host, method, g.prefix+path, handler, m...)
} }

View File

@ -79,14 +79,13 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
if i == l { if i == l {
r.insert(method, path[:i], h, pkind, ppath, pnames) r.insert(method, path[:i], h, pkind, ppath, pnames)
return } else {
r.insert(method, path[:i], nil, pkind, "", nil)
} }
r.insert(method, path[:i], nil, pkind, "", nil)
} else if path[i] == '*' { } else if path[i] == '*' {
r.insert(method, path[:i], nil, skind, "", nil) r.insert(method, path[:i], nil, skind, "", nil)
pnames = append(pnames, "*") pnames = append(pnames, "*")
r.insert(method, path[:i+1], h, akind, ppath, pnames) r.insert(method, path[:i+1], h, akind, ppath, pnames)
return
} }
} }