From e119cbc2ea0ac30f9739b720701a1b2ce3770a32 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Fri, 22 May 2015 21:24:35 -0700 Subject: [PATCH] Separated Group from Echo to limit API Signed-off-by: Vishal Rana --- echo.go | 24 ++++++++++++------------ group.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/echo.go b/echo.go index 1b95ee8a..0246f527 100644 --- a/echo.go +++ b/echo.go @@ -175,18 +175,6 @@ func New() (e *Echo) { return } -// Group creates a new sub router with prefix. It inherits all properties from -// the parent. Passing middleware overrides parent middleware. -func (e *Echo) Group(pfx string, m ...Middleware) *Echo { - g := *e - g.prefix = g.prefix + pfx - if len(m) > 0 { - g.middleware = nil - g.Use(m...) - } - return &g -} - // Router returns router. func (e *Echo) Router() *Router { return e.router @@ -355,6 +343,18 @@ func (e *Echo) URL(h Handler, params ...interface{}) string { return e.URI(h, params...) } +// Group creates a new sub router with prefix. It inherits all properties from +// the parent. Passing middleware overrides parent middleware. +func (e *Echo) Group(prefix string, m ...Middleware) *Group { + g := &Group{*e} + g.echo.prefix += prefix + if len(m) > 0 { + g.echo.middleware = nil + g.Use(m...) + } + return g +} + func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { c := e.pool.Get().(*Context) h, echo := e.router.Find(r.Method, r.URL.Path, c) diff --git a/group.go b/group.go index 18ad367e..18f8f19d 100644 --- a/group.go +++ b/group.go @@ -2,6 +2,56 @@ package echo type ( Group struct { - *Echo + echo Echo } ) + +func (g *Group) Use(m ...Middleware) { + for _, h := range m { + g.echo.middleware = append(g.echo.middleware, wrapMiddleware(h)) + } +} + +func (g *Group) Connect(path string, h Handler) { + g.echo.Connect(path, h) +} + +func (g *Group) Delete(path string, h Handler) { + g.echo.Delete(path, h) +} + +func (g *Group) Get(path string, h Handler) { + g.echo.Get(path, h) +} + +func (g *Group) Head(path string, h Handler) { + g.echo.Head(path, h) +} + +func (g *Group) Options(path string, h Handler) { + g.echo.Options(path, h) +} + +func (g *Group) Patch(path string, h Handler) { + g.echo.Patch(path, h) +} + +func (g *Group) Post(path string, h Handler) { + g.echo.Post(path, h) +} + +func (g *Group) Put(path string, h Handler) { + g.echo.Put(path, h) +} + +func (g *Group) Trace(path string, h Handler) { + g.echo.Trace(path, h) +} + +func (g *Group) WebSocket(path string, h HandlerFunc) { + g.echo.WebSocket(path, h) +} + +func (g *Group) Group(prefix string, m ...Middleware) *Group { + return g.echo.Group(prefix, m...) +}