diff --git a/context.go b/context.go index b1ded8931..30e8c5689 100644 --- a/context.go +++ b/context.go @@ -48,6 +48,11 @@ func (c *Context) Request() *http.Request { return c.request } +// SetResponse sets the context's response. +func (c *Context) SetRequest(req *http.Request) { + c.request = req +} + // Response returns *Response. func (c *Context) Response() *Response { return c.response diff --git a/echo.go b/echo.go index be6896953..cead4a976 100644 --- a/echo.go +++ b/echo.go @@ -173,7 +173,7 @@ var ( // Error handlers //---------------- - notFoundHandler = func(c *Context) error { + defaultNotFoundHandler = func(c *Context) error { return NewHTTPError(http.StatusNotFound) } @@ -212,6 +212,7 @@ func New() (e *Echo) { } e.logger.Error(err) } + e.SetNotFoundHandler(defaultNotFoundHandler) e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler) e.SetBinder(&binder{}) @@ -262,6 +263,11 @@ func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) { e.httpErrorHandler = h } +// SetNotFoundHandler registers a custom handlers used when no route was found. +func (e *Echo) SetNotFoundHandler(h HandlerFunc) { + e.notFoundHandler = h +} + // SetBinder registers a custom binder. It's invoked by Context.Bind(). func (e *Echo) SetBinder(b Binder) { e.binder = b @@ -576,6 +582,15 @@ func (e *Echo) RunTLSServer(s *http.Server, crtFile, keyFile string) { e.run(s, crtFile, keyFile) } +// Middlewares returns a list of middlewares used in this echo instance. +func (e *Echo) Middlewares() []Middleware { + ret := make([]Middleware, len(e.middleware)) + for i, m := range e.middleware { + ret[i] = m + } + return ret +} + func (e *Echo) run(s *http.Server, files ...string) { s.Handler = e // TODO: Remove in Go 1.6+ diff --git a/group.go b/group.go index 856a33651..18c52d7fd 100644 --- a/group.go +++ b/group.go @@ -12,6 +12,10 @@ func (g *Group) Use(m ...Middleware) { } } +func (g *Group) Any(path string, h Handler) { + g.echo.Any(path, h) +} + func (g *Group) Connect(path string, h Handler) { g.echo.Connect(path, h) } diff --git a/improbable_helpers.go b/improbable_helpers.go new file mode 100644 index 000000000..d425d1cfc --- /dev/null +++ b/improbable_helpers.go @@ -0,0 +1,46 @@ +// Copyright (c) 2015 All Right Reserved, Improbable Worlds Ltd. + +// Improbable custom handlers for stuff. + +package echo + +import ( + "bytes" +) + +// WithMiddleware wraps the given handler directly with a set of middleware (in last to first order). +func HandlerWithMiddleware(h Handler, middlewares ...Middleware) HandlerFunc { + wh := wrapHandler(h) + // Chain middleware with handler in the end + for i := len(middlewares) - 1; i >= 0; i-- { + m := middlewares[i] + wrappedM := wrapMiddleware(m) + wh = wrappedM(wh) + } + return wh +} + +// ParamMap returns a map of all parameters that result from the Path parameter expansion. +func (c *Context) ParamMap() map[string]string { + out := make(map[string]string) + for i, _ := range c.pnames { + out[c.pnames[i]] = c.pvalues[i] + } + return out +} + +// RenderWithContentType renders a template with data and sends the specified content type with +// response and status code. Templates can be registered using `Echo.SetRenderer()`. +func (c *Context) RenderWithContentType(code int, contentType string, name string, data interface{}) (err error) { + if c.echo.renderer == nil { + return RendererNotRegistered + } + buf := new(bytes.Buffer) + if err = c.echo.renderer.Render(buf, name, data); err != nil { + return + } + c.response.Header().Set(ContentType, contentType) + c.response.WriteHeader(code) + c.response.Write(buf.Bytes()) + return +} diff --git a/router.go b/router.go index a3bba24b2..b6ab2038b 100644 --- a/router.go +++ b/router.go @@ -272,12 +272,12 @@ func (n *node) check405() HandlerFunc { return methodNotAllowedHandler } } - return notFoundHandler + return n.echo.notFoundHandler } func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) { - h = notFoundHandler e = r.echo + h = e.notFoundHandler cn := r.tree // Current node as root var ( @@ -385,6 +385,8 @@ End: // NOTE: Slow zone... if h == nil { + // Make sure we have a reference to the not found handler. + cn.echo = e h = cn.check405() // Dig further for match-any, might have an empty value for *, e.g.