1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-04-18 16:59:58 -07:00
parent 1e04986e53
commit 1c2d6341ba
5 changed files with 120 additions and 35 deletions

View File

@ -59,7 +59,7 @@ import (
func main() {
e := echo.New()
e.Get("/", func(c echo.Context) error {
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Run(standard.New(":1323"))
@ -78,10 +78,10 @@ Hello, World! on the page.
### Routing
```go
e.Post("/users", saveUser)
e.Get("/users/:id", getUser)
e.Put("/users/:id", updateUser)
e.Delete("/users/:id", deleteUser)
e.POST("/users", saveUser)
e.GET("/users/:id", getUser)
e.PUT("/users/:id", updateUser)
e.DELETE("/users/:id", deleteUser)
```
### Path Parameters
@ -178,7 +178,7 @@ type User struct {
Email string `json:"email" xml:"email"`
}
e.Post("/users", func(c echo.Context) error {
e.POST("/users", func(c echo.Context) error {
u := new(User)
if err := c.Bind(u); err != nil {
return err
@ -224,7 +224,7 @@ track := func(next echo.HandlerFunc) echo.HandlerFunc {
return next(c)
}
}
e.Get("/users", func(c echo.Context) error {
e.GET("/users", func(c echo.Context) error {
return c.String(http.StatusOK, "/users")
}, track)
```

62
echo.go
View File

@ -27,7 +27,7 @@ Example:
e.Use(middleware.Recover())
// Routes
e.Get("/", hello)
e.GET("/", hello)
// Start server
e.Run(standard.New(":1323"))
@ -318,8 +318,13 @@ func (e *Echo) Use(middleware ...MiddlewareFunc) {
e.middleware = append(e.middleware, middleware...)
}
// Connect registers a new CONNECT route for a path with matching handler in the
// CONNECT registers a new CONNECT route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(CONNECT, path, h, m...)
}
// Connect is deprecated, use `CONNECT()` instead.
func (e *Echo) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(CONNECT, path, h, m...)
}
@ -330,44 +335,79 @@ func (e *Echo) Delete(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(DELETE, path, h, m...)
}
// Get registers a new GET route for a path with matching handler in the router
// GET registers a new GET route for a path with matching handler in the router
// with optional route-level middleware.
func (e *Echo) GET(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(GET, path, h, m...)
}
// Get is deprecated, use `GET()` instead.
func (e *Echo) Get(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(GET, path, h, m...)
}
// Head registers a new HEAD route for a path with matching handler in the
// HEAD registers a new HEAD route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(HEAD, path, h, m...)
}
// Head is deprecated, use `HEAD()` instead.
func (e *Echo) Head(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(HEAD, path, h, m...)
}
// Options registers a new OPTIONS route for a path with matching handler in the
// OPTIONS registers a new OPTIONS route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(OPTIONS, path, h, m...)
}
// Options is deprecated, use `OPTIONS()` instead.
func (e *Echo) Options(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(OPTIONS, path, h, m...)
}
// Patch registers a new PATCH route for a path with matching handler in the
// PATCH registers a new PATCH route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(PATCH, path, h, m...)
}
// Patch is deprecated, use `PATCH()` instead.
func (e *Echo) Patch(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(PATCH, path, h, m...)
}
// Post registers a new POST route for a path with matching handler in the
// POST registers a new POST route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) POST(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(POST, path, h, m...)
}
// Post is deprecated, use `POST()` instead.
func (e *Echo) Post(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(POST, path, h, m...)
}
// Put registers a new PUT route for a path with matching handler in the
// PUT registers a new PUT route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(PUT, path, h, m...)
}
// Put is deprecated, use `PUT()` instead.
func (e *Echo) Put(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(PUT, path, h, m...)
}
// Trace registers a new TRACE route for a path with matching handler in the
// TRACE registers a new TRACE route for a path with matching handler in the
// router with optional route-level middleware.
func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(TRACE, path, h, m...)
}
// Trace is deprecated, use `TRACE()` instead.
func (e *Echo) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(TRACE, path, h, m...)
}
@ -391,14 +431,14 @@ func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middlew
// Static registers a new route with path prefix to serve static files from the
// provided root directory.
func (e *Echo) Static(prefix, root string) {
e.Get(prefix+"*", func(c Context) error {
e.GET(prefix+"*", func(c Context) error {
return c.File(path.Join(root, c.P(0)))
})
}
// File registers a new route with path to serve a static file.
func (e *Echo) File(path, file string) {
e.Get(path, func(c Context) error {
e.GET(path, func(c Context) error {
return c.File(file)
})
}

View File

@ -105,7 +105,7 @@ func TestEchoMiddleware(t *testing.T) {
}))
// Route
e.Get("/", func(c Context) error {
e.GET("/", func(c Context) error {
return c.String(http.StatusOK, "OK")
})
@ -120,7 +120,7 @@ func TestEchoMiddlewareError(t *testing.T) {
e.Use(WrapMiddleware(func(c Context) error {
return errors.New("error")
}))
e.Get("/", notFoundHandler)
e.GET("/", notFoundHandler)
c, _ := request(GET, "/", e)
assert.Equal(t, http.StatusInternalServerError, c)
}
@ -129,7 +129,7 @@ func TestEchoHandler(t *testing.T) {
e := New()
// HandlerFunc
e.Get("/ok", func(c Context) error {
e.GET("/ok", func(c Context) error {
return c.String(http.StatusOK, "OK")
})
@ -203,8 +203,8 @@ func TestEchoURL(t *testing.T) {
getUser := func(Context) error { return nil }
getFile := func(Context) error { return nil }
e.Get("/static/file", static)
e.Get("/users/:id", getUser)
e.GET("/static/file", static)
e.GET("/users/:id", getUser)
g := e.Group("/group")
g.Get("/users/:uid/files/:fid", getFile)
@ -252,7 +252,7 @@ func TestEchoGroup(t *testing.T) {
// Routes
//--------
e.Get("/users", h)
e.GET("/users", h)
// Group
g1 := e.Group("/group1")
@ -297,7 +297,7 @@ func TestEchoNotFound(t *testing.T) {
func TestEchoMethodNotAllowed(t *testing.T) {
e := New()
e.Get("/", func(c Context) error {
e.GET("/", func(c Context) error {
return c.String(http.StatusOK, "Echo!")
})
rq := test.NewRequest(POST, "/", nil)

View File

@ -21,47 +21,92 @@ func (g *Group) Use(m ...MiddlewareFunc) {
}, g.middleware...)
}
// Connect implements `Echo#Connect()` for sub-routes within the Group.
// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
func (g *Group) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(CONNECT, path, h, m...)
}
// Connect is deprecated, use `CONNECT()` instead.
func (g *Group) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(CONNECT, path, h, m...)
}
// Delete implements `Echo#Delete()` for sub-routes within the Group.
// DELETE implements `Echo#DELETE()` for sub-routes within the Group.
func (g *Group) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(DELETE, path, h, m...)
}
// Delete is deprecated, use `DELETE()` instead.
func (g *Group) Delete(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(DELETE, path, h, m...)
}
// Get implements `Echo#Get()` for sub-routes within the Group.
// GET implements `Echo#GET()` for sub-routes within the Group.
func (g *Group) GET(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(GET, path, h, m...)
}
// Get is deprecated, use `GET()` instead.
func (g *Group) Get(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(GET, path, h, m...)
}
// Head implements `Echo#Head()` for sub-routes within the Group.
// HEAD implements `Echo#HEAD()` for sub-routes within the Group.
func (g *Group) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(HEAD, path, h, m...)
}
// Head is deprecated, use `HEAD()` instead.
func (g *Group) Head(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(HEAD, path, h, m...)
}
// Options implements `Echo#Options()` for sub-routes within the Group.
// OPTIONS implements `Echo#OPTIONS()` for sub-routes within the Group.
func (g *Group) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(OPTIONS, path, h, m...)
}
// Options is deprecated, use `OPTIONS()` instead.
func (g *Group) Options(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(OPTIONS, path, h, m...)
}
// Patch implements `Echo#Patch()` for sub-routes within the Group.
// PATCH implements `Echo#PATCH()` for sub-routes within the Group.
func (g *Group) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(PATCH, path, h, m...)
}
// Patch is deprecated, use `PATCH()` instead.
func (g *Group) Patch(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(PATCH, path, h, m...)
}
// Post implements `Echo#Post()` for sub-routes within the Group.
// POST implements `Echo#POST()` for sub-routes within the Group.
func (g *Group) POST(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(POST, path, h, m...)
}
// Post is deprecated, use `POST()` instead.
func (g *Group) Post(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(POST, path, h, m...)
}
// Put implements `Echo#Put()` for sub-routes within the Group.
// PUT implements `Echo#PUT()` for sub-routes within the Group.
func (g *Group) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(PUT, path, h, m...)
}
// Put is deprecated, use `PUT()` instead.
func (g *Group) Put(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(PUT, path, h, m...)
}
// Trace implements `Echo#Trace()` for sub-routes within the Group.
// TRACE implements `Echo#TRACE()` for sub-routes within the Group.
func (g *Group) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(TRACE, path, h, m...)
}
// Trace is deprecated, use `TRACE()` instead.
func (g *Group) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
g.add(TRACE, path, h, m...)
}

View File

@ -65,7 +65,7 @@ func TestGzipNoContent(t *testing.T) {
func TestGzipErrorReturned(t *testing.T) {
e := echo.New()
e.Use(Gzip())
e.Get("/", func(c echo.Context) error {
e.GET("/", func(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "error")
})
rq := test.NewRequest(echo.GET, "/", nil)