Skip to content

Commit

Permalink
New renderer interface #21
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Apr 9, 2015
1 parent da18bc1 commit 6633a1f
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 309 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *Context) Bind(v interface{}) error {
func (c *Context) Render(code int, name string, data interface{}) error {
c.Response.Header().Set(HeaderContentType, MIMEHTML+"; charset=utf-8")
c.Response.WriteHeader(code)
return c.echo.renderFunc(c.Response, name, data)
return c.echo.renderer.Render(c.Response, name, data)
}

// JSON sends an application/json response with status code.
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestContext(t *testing.T) {
b, _ := json.Marshal(u1)
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
c := &Context{
Response: &response{writer: httptest.NewRecorder()},
Response: &response{Writer: httptest.NewRecorder()},
Request: r,
params: make(Params, 5),
store: make(store),
Expand Down
18 changes: 9 additions & 9 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ type (
middleware []MiddlewareFunc
maxParam byte
notFoundHandler HandlerFunc
renderFunc RenderFunc
renderer Renderer
pool sync.Pool
}
Middleware interface{}
MiddlewareFunc func(HandlerFunc) HandlerFunc
Handler interface{}
HandlerFunc func(*Context)
RenderFunc func(io.Writer, string, interface{}) error
Renderer interface {
Render(io.Writer, string, interface{}) error
}
)

const (
Expand Down Expand Up @@ -72,9 +74,6 @@ func New() (e *Echo) {
notFoundHandler: func(c *Context) {
http.Error(c.Response, http.StatusText(http.StatusNotFound), http.StatusNotFound)
},
renderFunc: func(w io.Writer, name string, data interface{}) (err error) {
return
},
}
e.Router = NewRouter(e)
e.pool.New = func() interface{} {
Expand Down Expand Up @@ -115,9 +114,9 @@ func (e *Echo) NotFoundHandler(h Handler) {
e.notFoundHandler = wrapH(h)
}

// RenderFunc sets a custom RenderFunc.
func (e *Echo) RenderFunc(r RenderFunc) {
e.renderFunc = r
// Renderer sets an HTML Renderer.
func (e *Echo) Renderer(r Renderer) {
e.renderer = r
}

// Use adds handler to the middleware chain.
Expand Down Expand Up @@ -197,7 +196,8 @@ func (e *Echo) Index(file string) {
}

func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h, c, echo := e.Router.Find(r.Method, r.URL.Path)
c := e.pool.Get().(*Context)
h, echo := e.Router.Find(r.Method, r.URL.Path, c.params)
if echo != nil {
e = echo
}
Expand Down
10 changes: 5 additions & 5 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

type (
response struct {
writer http.ResponseWriter
Writer http.ResponseWriter
status int
size int
committed bool
}
)

func (r *response) Header() http.Header {
return r.writer.Header()
return r.Writer.Header()
}

func (r *response) WriteHeader(n int) {
Expand All @@ -25,12 +25,12 @@ func (r *response) WriteHeader(n int) {
return
}
r.status = n
r.writer.WriteHeader(n)
r.Writer.WriteHeader(n)
r.committed = true
}

func (r *response) Write(b []byte) (n int, err error) {
n, err = r.writer.Write(b)
n, err = r.Writer.Write(b)
r.size += n
return n, err
}
Expand All @@ -44,6 +44,6 @@ func (r *response) Size() int {
}

func (r *response) reset(w http.ResponseWriter) {
r.writer = w
r.Writer = w
r.committed = false
}
12 changes: 6 additions & 6 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ func lcp(a, b string) (i int) {
return
}

func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Echo) {
c = r.echo.pool.Get().(*Context)
func (r *router) Find(method, path string, params Params) (h HandlerFunc, echo *Echo) {
cn := r.trees[method] // Current node as root
search := path
n := 0 // Param count
Expand All @@ -182,15 +181,15 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
p := c.params[:n+1]
p := params[:n+1]
p[n].Name = cn.prefix[1:]
p[n].Value = search[:i]
n++
search = search[i:]
} else if cn.has == cnode {
// Catch-all node
cn = cn.edges[0]
p := c.params[:n+1]
p := params[:n+1]
p[n].Name = "_name"
p[n].Value = search
search = "" // End search
Expand All @@ -215,8 +214,9 @@ func (r *router) Find(method, path string) (h HandlerFunc, c *Context, echo *Ech
}

func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h, c, _ := r.Find(req.Method, req.URL.Path)
c.Response.writer = w
c := r.echo.pool.Get().(*Context)
h, _ := r.Find(req.Method, req.URL.Path, c.params)
c.Response.Writer = w
if h != nil {
h(c)
} else {
Expand Down
Loading

0 comments on commit 6633a1f

Please sign in to comment.