Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add improbable helpers #1

Open
wants to merge 3 commits into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ var (
// Error handlers
//----------------

notFoundHandler = func(c *Context) error {
defaultNotFoundHandler = func(c *Context) error {
return NewHTTPError(http.StatusNotFound)
}

Expand Down Expand Up @@ -212,6 +212,7 @@ func New() (e *Echo) {
}
e.logger.Error(err)
}
e.SetNotFoundHandler(defaultNotFoundHandler)
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
e.SetBinder(&binder{})

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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+
Expand Down
4 changes: 4 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
46 changes: 46 additions & 0 deletions improbable_helpers.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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.
Expand Down