Skip to content

Commit

Permalink
Fixed labstack#162
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Jul 27, 2015
1 parent 1ae7ef4 commit d077efe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
8 changes: 4 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *Context) Bind(i interface{}) error {

// Render renders a template with data and sends a text/html response with status
// code. Templates can be registered using `Echo.SetRenderer()`.
func (c *Context) Render(code int, name string, data interface{}) (err error) {
func (c *Context) Render(code int, name string, data interface{}) error {
if c.echo.renderer == nil {
return RendererNotRegistered
}
Expand All @@ -123,7 +123,7 @@ func (c *Context) Render(code int, name string, data interface{}) (err error) {
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
c.response.WriteHeader(code)
_, err = fmt.Fprintf(c.response, format, a...)
_, err = fmt.Fprintf(c.response, format, a...)
return
}

Expand All @@ -137,7 +137,7 @@ func (c *Context) String(code int, format string, a ...interface{}) (err error)
}

// JSON sends a JSON response with status code.
func (c *Context) JSON(code int, i interface{}) (err error) {
func (c *Context) JSON(code int, i interface{}) error {
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
return json.NewEncoder(c.response).Encode(i)
Expand All @@ -156,7 +156,7 @@ func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
}

// XML sends an XML response with status code.
func (c *Context) XML(code int, i interface{}) (err error) {
func (c *Context) XML(code int, i interface{}) error {
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
c.response.WriteHeader(code)
c.response.Write([]byte(xml.Header))
Expand Down
19 changes: 6 additions & 13 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,14 @@ const (
)

var (
methods = [...]string{
CONNECT,
DELETE,
GET,
HEAD,
OPTIONS,
PATCH,
POST,
PUT,
TRACE,
}

//--------
// Errors
//--------

UnsupportedMediaType = errors.New("echo ⇒ unsupported media type")
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
InvalidRedirectCode = errors.New("echo ⇒ invalid redirect status code")

//----------------
// Error handlers
//----------------
Expand Down Expand Up @@ -222,7 +211,11 @@ func (e *Echo) Router() *Router {

// ColoredLog enable/disable colored log.
func (e *Echo) ColoredLog(on bool) {
color.Disable()
if on {
color.Enable()
} else {
color.Disable()
}
}

// HTTP2 enable/disable HTTP2 support.
Expand Down
19 changes: 10 additions & 9 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ const (
mtype
)

func NewRouter(e *Echo) (r *Router) {
r = &Router{
routes: []Route{},
echo: e,
func NewRouter(e *Echo) *Router {
return &Router{
connectTree: new(node),
deleteTree: new(node),
getTree: new(node),
Expand All @@ -52,8 +50,9 @@ func NewRouter(e *Echo) (r *Router) {
postTree: new(node),
putTree: new(node),
traceTree: new(node),
routes: []Route{},
echo: e,
}
return
}

func (r *Router) Add(method, path string, h HandlerFunc, e *Echo) {
Expand Down Expand Up @@ -307,10 +306,12 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
// Search order static > param > match-any
for {
if search == "" {
// Found
ctx.pnames = cn.pnames
h = cn.handler
e = cn.echo
if cn.handler != nil {
// Found
ctx.pnames = cn.pnames
h = cn.handler
e = cn.echo
}
return
}

Expand Down
3 changes: 0 additions & 3 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,6 @@ func TestRouterTwoParam(t *testing.T) {
assert.Equal(t, "1", c.P(0))
assert.Equal(t, "1", c.P(1))
}

h, _ = r.Find(GET, "/users/1", c)
assert.Nil(t, h)
}

func TestRouterMatchAny(t *testing.T) {
Expand Down

0 comments on commit d077efe

Please sign in to comment.