Skip to content

Commit 5a71f20

Browse files
committed
Updated docs, fixed #225.
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent c020919 commit 5a71f20

15 files changed

Lines changed: 80 additions & 32 deletions

File tree

context.go

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212

1313
"golang.org/x/net/websocket"
14+
"bytes"
1415
)
1516

1617
type (
@@ -110,68 +111,99 @@ func (c *Context) Bind(i interface{}) error {
110111

111112
// Render renders a template with data and sends a text/html response with status
112113
// code. Templates can be registered using `Echo.SetRenderer()`.
113-
func (c *Context) Render(code int, name string, data interface{}) error {
114+
func (c *Context) Render(code int, name string, data interface{}) (err error) {
114115
if c.echo.renderer == nil {
115116
return RendererNotRegistered
116117
}
118+
buf := new (bytes.Buffer)
119+
if err = c.echo.renderer.Render(buf, name, data); err != nil {
120+
return
121+
}
117122
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
118123
c.response.WriteHeader(code)
119-
return c.echo.renderer.Render(c.response, name, data)
124+
c.response.Write(buf.Bytes())
125+
return
120126
}
121127

122128
// HTML formats according to a format specifier and sends HTML response with
123129
// status code.
124130
func (c *Context) HTML(code int, format string, a ...interface{}) (err error) {
131+
buf := new(bytes.Buffer)
132+
_, err = fmt.Fprintf(buf, format, a...)
133+
if err != nil {
134+
return err
135+
}
125136
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
126137
c.response.WriteHeader(code)
127-
_, err = fmt.Fprintf(c.response, format, a...)
138+
c.response.Write(buf.Bytes())
128139
return
129140
}
130141

131142
// String formats according to a format specifier and sends text response with status
132143
// code.
133144
func (c *Context) String(code int, format string, a ...interface{}) (err error) {
145+
buf := new(bytes.Buffer)
146+
_, err = fmt.Fprintf(buf, format, a...)
147+
if err != nil {
148+
return err
149+
}
134150
c.response.Header().Set(ContentType, TextPlain)
135151
c.response.WriteHeader(code)
136-
_, err = fmt.Fprintf(c.response, format, a...)
152+
c.response.Write(buf.Bytes())
137153
return
138154
}
139155

140156
// JSON sends a JSON response with status code.
141-
func (c *Context) JSON(code int, i interface{}) error {
157+
func (c *Context) JSON(code int, i interface{}) (err error) {
158+
b, err := json.Marshal(i)
159+
if err != nil {
160+
return err
161+
}
142162
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
143163
c.response.WriteHeader(code)
144-
return json.NewEncoder(c.response).Encode(i)
164+
c.response.Write(b)
165+
return
145166
}
146167

147168
// JSONP sends a JSONP response with status code. It uses `callback` to construct
148169
// the JSONP payload.
149170
func (c *Context) JSONP(code int, callback string, i interface{}) (err error) {
171+
b, err := json.Marshal(i)
172+
if err != nil {
173+
return err
174+
}
150175
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
151176
c.response.WriteHeader(code)
152177
c.response.Write([]byte(callback + "("))
153-
if err = json.NewEncoder(c.response).Encode(i); err == nil {
154-
c.response.Write([]byte(");"))
155-
}
178+
c.response.Write(b)
179+
c.response.Write([]byte(");"))
156180
return
157181
}
158182

159183
// XML sends an XML response with status code.
160-
func (c *Context) XML(code int, i interface{}) error {
184+
func (c *Context) XML(code int, i interface{}) (err error) {
185+
b, err := xml.Marshal(i)
186+
if err != nil {
187+
return err
188+
}
161189
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
162190
c.response.WriteHeader(code)
163191
c.response.Write([]byte(xml.Header))
164-
return xml.NewEncoder(c.response).Encode(i)
192+
c.response.Write(b)
193+
return
165194
}
166195

167196
// File sends a response with the content of the file. If attachment is true, the
168197
// client is prompted to save the file.
169-
func (c *Context) File(name string, attachment bool) error {
198+
func (c *Context) File(name string, attachment bool) (err error) {
170199
dir, file := path.Split(name)
171200
if attachment {
172201
c.response.Header().Set(ContentDisposition, "attachment; filename="+file)
173202
}
174-
return serveFile(dir, file, c)
203+
if err = serveFile(dir, file, c); err != nil {
204+
c.response.Header().Del(ContentDisposition)
205+
}
206+
return
175207
}
176208

177209
// NoContent sends a response with no body and a status code.

context_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestContext(t *testing.T) {
9494
if assert.NoError(t, err) {
9595
assert.Equal(t, http.StatusOK, rec.Code)
9696
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
97-
assert.Equal(t, userJSON+"\n", rec.Body.String())
97+
assert.Equal(t, userJSON, rec.Body.String())
9898
}
9999

100100
// JSONP
@@ -105,7 +105,7 @@ func TestContext(t *testing.T) {
105105
if assert.NoError(t, err) {
106106
assert.Equal(t, http.StatusOK, rec.Code)
107107
assert.Equal(t, ApplicationJavaScriptCharsetUTF8, rec.Header().Get(ContentType))
108-
assert.Equal(t, callback+"("+userJSON+"\n);", rec.Body.String())
108+
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
109109
}
110110

111111
// XML

website/config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"menu": {
77
"main": [{
88
"Name": "Guide",
9-
"Pre": "<i class='fa fa-heart'></i>",
9+
"Pre": "<i class='fa fa-book'></i>",
1010
"Weight": -110,
1111
"Identifier": "guide",
1212
"URL": "guide"
1313
}, {
1414
"Name": "Recipes",
15-
"Pre": "<i class='fa fa-road'></i>",
15+
"Pre": "<i class='fa fa-code'></i>",
1616
"Weight": -100,
1717
"Identifier": "recipes",
1818
"URL": "recipes"

website/content/guide/customization.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Customization
33
menu:
44
main:
55
parent: guide
6+
weight: 20
67
---
78

89
### HTTP error handler

website/content/guide/error-handling.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Error Handling
33
menu:
44
main:
55
parent: guide
6+
weight: 70
67
---
78

89
Echo advocates centralized HTTP error handling by returning `error` from middleware

website/content/guide/installation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Installation
33
menu:
44
main:
55
parent: guide
6+
weight: 10
67
---
78

89
Echo has been developed and tested using Go `1.4.x`

website/content/guide/middleware.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Middleware
33
menu:
44
main:
55
parent: guide
6+
weight: 40
67
---
78

89
Middleware is a function which is chained in the HTTP request-response cycle. Middleware

website/content/guide/request.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Request
33
menu:
44
main:
55
parent: guide
6+
weight: 50
67
---
78

89
### Path parameter

website/content/guide/response.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Response
33
menu:
44
main:
55
parent: guide
6+
weight: 60
67
---
78

89
### Template

website/content/guide/routing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Routing
33
menu:
44
main:
55
parent: guide
6+
weight: 30
67
---
78

89
Echo's router is [fast, optimized](https://github.com/labstack/echo#benchmark) and

0 commit comments

Comments
 (0)