Skip to content

Commit 1e11762

Browse files
committed
Merge pull request #76 from labstack/websocket
New definition for Echo.HandlerFunc
2 parents a451812 + 1ad37ae commit 1e11762

23 files changed

+267
-215
lines changed

README.md

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
99
- `echo.MiddlewareFunc`
1010
- `func(echo.HandlerFunc) echo.HandlerFunc`
1111
- `echo.HandlerFunc`
12-
- `func(*echo.Context) *echo.HTTPError`
12+
- `func(*echo.Context) error`
1313
- `func(http.Handler) http.Handler`
1414
- `http.Handler`
1515
- `http.HandlerFunc`
1616
- `func(http.ResponseWriter, *http.Request)`
1717
- Handler
1818
- `echo.HandlerFunc`
19-
- `func(*echo.Context) *echo.HTTPError`
19+
- `func(*echo.Context) error`
2020
- `http.Handler`
2121
- `http.HandlerFunc`
2222
- `func(http.ResponseWriter, *http.Request)`
@@ -84,7 +84,7 @@ import (
8484
)
8585

8686
// Handler
87-
func hello(c *echo.Context) *echo.HTTPError {
87+
func hello(c *echo.Context) error {
8888
return c.String(http.StatusOK, "Hello, World!\n")
8989
}
9090

@@ -133,34 +133,34 @@ var (
133133
// Handlers
134134
//----------
135135

136-
func createUser(c *echo.Context) *echo.HTTPError {
136+
func createUser(c *echo.Context) error {
137137
u := &user{
138138
ID: seq,
139139
}
140-
if he := c.Bind(u); he != nil {
141-
return he
140+
if err := c.Bind(u); err != nil {
141+
return err
142142
}
143143
users[u.ID] = u
144144
seq++
145145
return c.JSON(http.StatusCreated, u)
146146
}
147147

148-
func getUser(c *echo.Context) *echo.HTTPError {
148+
func getUser(c *echo.Context) error {
149149
id, _ := strconv.Atoi(c.Param("id"))
150150
return c.JSON(http.StatusOK, users[id])
151151
}
152152

153-
func updateUser(c *echo.Context) *echo.HTTPError {
153+
func updateUser(c *echo.Context) error {
154154
u := new(user)
155-
if he := c.Bind(u); he != nil {
156-
return he
155+
if err := c.Bind(u); err != nil {
156+
return err
157157
}
158158
id, _ := strconv.Atoi(c.Param("id"))
159159
users[id].Name = u.Name
160160
return c.JSON(http.StatusOK, users[id])
161161
}
162162

163-
func deleteUser(c *echo.Context) *echo.HTTPError {
163+
func deleteUser(c *echo.Context) error {
164164
id, _ := strconv.Atoi(c.Param("id"))
165165
delete(users, id)
166166
return c.NoContent(http.StatusNoContent)
@@ -218,22 +218,19 @@ var (
218218
)
219219

220220
// Render HTML
221-
func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTPError {
222-
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
223-
return &echo.HTTPError{Error: err}
224-
}
225-
return nil
221+
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
222+
return t.templates.ExecuteTemplate(w, name, data)
226223
}
227224

228225
//----------
229226
// Handlers
230227
//----------
231228

232-
func welcome(c *echo.Context) *echo.HTTPError {
229+
func welcome(c *echo.Context) error {
233230
return c.Render(http.StatusOK, "welcome", "Joe")
234231
}
235232

236-
func createUser(c *echo.Context) *echo.HTTPError {
233+
func createUser(c *echo.Context) error {
237234
u := new(user)
238235
if err := c.Bind(u); err != nil {
239236
return err
@@ -242,11 +239,11 @@ func createUser(c *echo.Context) *echo.HTTPError {
242239
return c.JSON(http.StatusCreated, u)
243240
}
244241

245-
func getUsers(c *echo.Context) *echo.HTTPError {
242+
func getUsers(c *echo.Context) error {
246243
return c.JSON(http.StatusOK, users)
247244
}
248245

249-
func getUser(c *echo.Context) *echo.HTTPError {
246+
func getUser(c *echo.Context) error {
250247
return c.JSON(http.StatusOK, users[c.P(0)])
251248
}
252249

@@ -268,7 +265,7 @@ func main() {
268265
s := stats.New()
269266
e.Use(s.Handler)
270267
// Route
271-
e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
268+
e.Get("/stats", func(c *echo.Context) error {
272269
return c.JSON(http.StatusOK, s.Data())
273270
})
274271

@@ -297,7 +294,7 @@ func main() {
297294
// Cached templates
298295
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
299296
}
300-
e.Renderer(t)
297+
e.SetRenderer(t)
301298
e.Get("/welcome", welcome)
302299

303300
//-------
@@ -306,20 +303,20 @@ func main() {
306303

307304
// Group with parent middleware
308305
a := e.Group("/admin")
309-
a.Use(func(c *echo.Context) *echo.HTTPError {
306+
a.Use(func(c *echo.Context) error {
310307
// Security middleware
311308
return nil
312309
})
313-
a.Get("", func(c *echo.Context) *echo.HTTPError {
310+
a.Get("", func(c *echo.Context) error {
314311
return c.String(http.StatusOK, "Welcome admin!")
315312
})
316313

317314
// Group with no parent middleware
318-
g := e.Group("/files", func(c *echo.Context) *echo.HTTPError {
315+
g := e.Group("/files", func(c *echo.Context) error {
319316
// Security middleware
320317
return nil
321318
})
322-
g.Get("", func(c *echo.Context) *echo.HTTPError {
319+
g.Get("", func(c *echo.Context) error {
323320
return c.String(http.StatusOK, "Your files!")
324321
})
325322

@@ -350,7 +347,7 @@ import (
350347
)
351348

352349
// Handler
353-
func hello(c *echo.Context) *echo.HTTPError {
350+
func hello(c *echo.Context) error {
354351
return c.String(http.StatusOK, "Hello, World!\n")
355352
}
356353

@@ -359,7 +356,7 @@ func main() {
359356
e := echo.New()
360357

361358
// Debug mode
362-
e.Debug(true)
359+
e.SetDebug(true)
363360

364361
//------------
365362
// Middleware

context.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package echo
33
import (
44
"encoding/json"
55
"net/http"
6+
7+
"golang.org/x/net/websocket"
68
)
79

810
type (
@@ -11,6 +13,7 @@ type (
1113
Context struct {
1214
Request *http.Request
1315
Response *Response
16+
Socket *websocket.Conn
1417
pnames []string
1518
pvalues []string
1619
store store
@@ -53,60 +56,53 @@ func (c *Context) Param(name string) (value string) {
5356

5457
// Bind binds the request body into specified type v. Default binder does it
5558
// based on Content-Type header.
56-
func (c *Context) Bind(i interface{}) *HTTPError {
59+
func (c *Context) Bind(i interface{}) error {
5760
return c.echo.binder(c.Request, i)
5861
}
5962

6063
// Render invokes the registered HTML template renderer and sends a text/html
6164
// response with status code.
62-
func (c *Context) Render(code int, name string, data interface{}) *HTTPError {
65+
func (c *Context) Render(code int, name string, data interface{}) error {
6366
if c.echo.renderer == nil {
64-
return &HTTPError{Error: RendererNotRegistered}
67+
return RendererNotRegistered
6568
}
6669
c.Response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
6770
c.Response.WriteHeader(code)
6871
return c.echo.renderer.Render(c.Response, name, data)
6972
}
7073

7174
// JSON sends an application/json response with status code.
72-
func (c *Context) JSON(code int, i interface{}) *HTTPError {
75+
func (c *Context) JSON(code int, i interface{}) error {
7376
c.Response.Header().Set(ContentType, ApplicationJSON+"; charset=utf-8")
7477
c.Response.WriteHeader(code)
75-
if err := json.NewEncoder(c.Response).Encode(i); err != nil {
76-
return &HTTPError{Error: err}
77-
}
78-
return nil
78+
return json.NewEncoder(c.Response).Encode(i)
7979
}
8080

8181
// String sends a text/plain response with status code.
82-
func (c *Context) String(code int, s string) *HTTPError {
82+
func (c *Context) String(code int, s string) error {
8383
c.Response.Header().Set(ContentType, TextPlain+"; charset=utf-8")
8484
c.Response.WriteHeader(code)
85-
if _, err := c.Response.Write([]byte(s)); err != nil {
86-
return &HTTPError{Error: err}
87-
}
88-
return nil
85+
_, err := c.Response.Write([]byte(s))
86+
return err
8987
}
9088

9189
// HTML sends a text/html response with status code.
92-
func (c *Context) HTML(code int, html string) *HTTPError {
90+
func (c *Context) HTML(code int, html string) error {
9391
c.Response.Header().Set(ContentType, TextHTML+"; charset=utf-8")
9492
c.Response.WriteHeader(code)
95-
if _, err := c.Response.Write([]byte(html)); err != nil {
96-
return &HTTPError{Error: err}
97-
}
98-
return nil
93+
_, err := c.Response.Write([]byte(html))
94+
return err
9995
}
10096

10197
// NoContent sends a response with no body and a status code.
102-
func (c *Context) NoContent(code int) *HTTPError {
98+
func (c *Context) NoContent(code int) error {
10399
c.Response.WriteHeader(code)
104100
return nil
105101
}
106102

107103
// Error invokes the registered HTTP error handler.
108-
func (c *Context) Error(he *HTTPError) {
109-
c.echo.httpErrorHandler(he, c)
104+
func (c *Context) Error(err error) {
105+
c.echo.httpErrorHandler(err, c)
110106
}
111107

112108
// Get retrieves data from the context.

context_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,14 @@ type (
1616
}
1717
)
1818

19-
func (t *Template) Render(w io.Writer, name string, data interface{}) *HTTPError {
20-
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
21-
return &HTTPError{Error: err}
22-
}
23-
return nil
19+
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
20+
return t.templates.ExecuteTemplate(w, name, data)
2421
}
2522

2623
func TestContext(t *testing.T) {
2724
b, _ := json.Marshal(u1)
2825
r, _ := http.NewRequest(POST, "/users/1", bytes.NewReader(b))
29-
c := NewContext(r, &Response{Writer: httptest.NewRecorder()}, New())
26+
c := NewContext(r, NewResponse(httptest.NewRecorder()), New())
3027

3128
//------
3229
// Bind

0 commit comments

Comments
 (0)