Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 2, 2016
1 parent be5148a commit b5d6c05
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 300 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ import (
"github.com/labstack/echo/middleware"
)

// Handler
func hello() echo.HandlerFunc {
return func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!\n")
}
}

func main() {
// Echo instance
e := echo.New()
Expand All @@ -65,8 +58,10 @@ func main() {
e.Use(middleware.Logger())
e.Use(middleware.Recover())

// Routes
e.Get("/", hello())
// Route => handler
e.Get("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!\n")
})

// Start server
e.Run(standard.New(":1323"))
Expand Down Expand Up @@ -96,7 +91,7 @@ Hello, World! on the page.

- :star: the project
- [Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=JD5R56K84A8G4&lc=US&item_name=LabStack&item_number=echo&currency_code=USD&bn=PP-DonationsBF:btn_donate_LG.gif:NonHosted)
- :earth_americas: spread the word
- :earth_americas: spread the word
- [Contribute](#contribute) to the project

## Contribute
Expand Down
4 changes: 2 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ type (
pvalues []string
query url.Values
store store
handler Handler
handler HandlerFunc
echo *Echo
}

Expand Down Expand Up @@ -204,7 +204,7 @@ func (c *context) Value(key interface{}) interface{} {
}

func (c *context) Handle(ctx Context) error {
return c.handler.Handle(ctx)
return c.handler(ctx)
}

func (c *context) Request() engine.Request {
Expand Down
124 changes: 50 additions & 74 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ type (
// Echo is the top-level framework instance.
Echo struct {
prefix string
middleware []Middleware
head Handler
pristineHead Handler
middleware []MiddlewareFunc
head HandlerFunc
pristineHead HandlerFunc
maxParam *int
notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler
Expand All @@ -89,24 +89,10 @@ type (
Message string
}

// Middleware defines an interface for middleware via `Handle(Handler) Handler`
// function.
Middleware interface {
Handle(Handler) Handler
}

// MiddlewareFunc is an adapter to allow the use of `func(Handler) Handler` as
// middleware.
MiddlewareFunc func(Handler) Handler
// MiddlewareFunc defines a function to process middleware.
MiddlewareFunc func(HandlerFunc) HandlerFunc

// Handler defines an interface to server HTTP requests via `Handle(Context)`
// function.
Handler interface {
Handle(Context) error
}

// HandlerFunc is an adapter to allow the use of `func(Context)` as an HTTP
// handler.
// HandlerFunc defines a function to server HTTP requests.
HandlerFunc func(Context) error

// HTTPErrorHandler is a centralized HTTP error handler.
Expand Down Expand Up @@ -212,13 +198,13 @@ var (

// Error handlers
var (
notFoundHandler = HandlerFunc(func(c Context) error {
notFoundHandler = func(c Context) error {
return ErrNotFound
})
}

methodNotAllowedHandler = HandlerFunc(func(c Context) error {
methodNotAllowedHandler = func(c Context) error {
return ErrMethodNotAllowed
})
}
)

// New creates an instance of Echo.
Expand All @@ -228,10 +214,10 @@ func New() (e *Echo) {
return NewContext(nil, nil, e)
}
e.router = NewRouter(e)
e.middleware = []Middleware{e.router}
e.head = HandlerFunc(func(c Context) error {
e.middleware = []MiddlewareFunc{e.router.Process}
e.head = func(c Context) error {
return c.Handle(c)
})
}
e.pristineHead = e.head
e.chainMiddleware()

Expand All @@ -244,16 +230,6 @@ func New() (e *Echo) {
return
}

// Handle chains middleware.
func (f MiddlewareFunc) Handle(h Handler) Handler {
return f(h)
}

// Handle serves HTTP request.
func (f HandlerFunc) Handle(c Context) error {
return f(c)
}

// Router returns router.
func (e *Echo) Router() *Router {
return e.router
Expand Down Expand Up @@ -323,118 +299,118 @@ func (e *Echo) Debug() bool {
}

// Pre adds middleware to the chain which is run before router.
func (e *Echo) Pre(middleware ...Middleware) {
func (e *Echo) Pre(middleware ...MiddlewareFunc) {
e.middleware = append(middleware, e.middleware...)
e.chainMiddleware()
}

// Use adds middleware to the chain which is run after router.
func (e *Echo) Use(middleware ...Middleware) {
func (e *Echo) Use(middleware ...MiddlewareFunc) {
e.middleware = append(e.middleware, middleware...)
e.chainMiddleware()
}

func (e *Echo) chainMiddleware() {
e.head = e.pristineHead
for i := len(e.middleware) - 1; i >= 0; i-- {
e.head = e.middleware[i].Handle(e.head)
e.head = e.middleware[i](e.head)
}
}

// 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 Handler, m ...Middleware) {
func (e *Echo) Connect(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(CONNECT, path, h, m...)
}

// Delete registers a new DELETE route for a path with matching handler in the router
// with optional route-level middleware.
func (e *Echo) Delete(path string, h Handler, m ...Middleware) {
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
// with optional route-level middleware.
func (e *Echo) Get(path string, h Handler, m ...Middleware) {
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
// router with optional route-level middleware.
func (e *Echo) Head(path string, h Handler, m ...Middleware) {
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
// router with optional route-level middleware.
func (e *Echo) Options(path string, h Handler, m ...Middleware) {
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
// router with optional route-level middleware.
func (e *Echo) Patch(path string, h Handler, m ...Middleware) {
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
// router with optional route-level middleware.
func (e *Echo) Post(path string, h Handler, m ...Middleware) {
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
// router with optional route-level middleware.
func (e *Echo) Put(path string, h Handler, m ...Middleware) {
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
// router with optional route-level middleware.
func (e *Echo) Trace(path string, h Handler, m ...Middleware) {
func (e *Echo) Trace(path string, h HandlerFunc, m ...MiddlewareFunc) {
e.add(TRACE, path, h, m...)
}

// Any registers a new route for all HTTP methods and path with matching handler
// in the router with optional route-level middleware.
func (e *Echo) Any(path string, handler Handler, middleware ...Middleware) {
func (e *Echo) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
for _, m := range methods {
e.add(m, path, handler, middleware...)
}
}

// Match registers a new route for multiple HTTP methods and path with matching
// handler in the router with optional route-level middleware.
func (e *Echo) Match(methods []string, path string, handler Handler, middleware ...Middleware) {
func (e *Echo) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
for _, m := range methods {
e.add(m, path, handler, middleware...)
}
}

// Static serves files from provided `root` directory for `/<prefix>*` HTTP path.
func (e *Echo) Static(prefix, root string) {
e.Get(prefix+"*", HandlerFunc(func(c Context) error {
e.Get(prefix+"*", func(c Context) error {
return c.File(path.Join(root, c.P(0))) // Param `_`
}))
})
}

// File serves provided file for `/<path>` HTTP path.
func (e *Echo) File(path, file string) {
e.Get(path, HandlerFunc(func(c Context) error {
e.Get(path, func(c Context) error {
return c.File(file)
}))
})
}

func (e *Echo) add(method, path string, handler Handler, middleware ...Middleware) {
func (e *Echo) add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
name := handlerName(handler)
e.router.Add(method, path, HandlerFunc(func(c Context) error {
e.router.Add(method, path, func(c Context) error {
h := handler
// Chain middleware
for i := len(middleware) - 1; i >= 0; i-- {
h = middleware[i].Handle(h)
h = middleware[i](h)
}
return h.Handle(c)
}), e)
return h(c)
}, e)
r := Route{
Method: method,
Path: path,
Expand All @@ -444,18 +420,18 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar
}

// Group creates a new router group with prefix and optional group-level middleware.
func (e *Echo) Group(prefix string, m ...Middleware) (g *Group) {
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
g = &Group{prefix: prefix, echo: e}
g.Use(m...)
// Dummy handler so group can be used with static middleware.
g.Get("", HandlerFunc(func(c Context) error {
g.Get("", func(c Context) error {
return c.NoContent(http.StatusNotFound)
}))
})
return
}

// URI generates a URI from handler.
func (e *Echo) URI(handler Handler, params ...interface{}) string {
func (e *Echo) URI(handler HandlerFunc, params ...interface{}) string {
uri := new(bytes.Buffer)
ln := len(params)
n := 0
Expand All @@ -480,7 +456,7 @@ func (e *Echo) URI(handler Handler, params ...interface{}) string {
}

// URL is an alias for `URI` function.
func (e *Echo) URL(h Handler, params ...interface{}) string {
func (e *Echo) URL(h HandlerFunc, params ...interface{}) string {
return e.URI(h, params...)
}

Expand All @@ -506,7 +482,7 @@ func (e *Echo) ServeHTTP(rq engine.Request, rs engine.Response) {
c.Reset(rq, rs)

// Execute chain
if err := e.head.Handle(c); err != nil {
if err := e.head(c); err != nil {
e.httpErrorHandler(err, c)
}

Expand Down Expand Up @@ -551,19 +527,19 @@ func (binder) Bind(i interface{}, c Context) (err error) {
return
}

// WrapMiddleware wrap `echo.Handler` into `echo.MiddlewareFunc`.
func WrapMiddleware(h Handler) MiddlewareFunc {
return func(next Handler) Handler {
return HandlerFunc(func(c Context) error {
if err := h.Handle(c); err != nil {
// WrapMiddleware wrap `echo.HandlerFunc` into `echo.MiddlewareFunc`.
func WrapMiddleware(h HandlerFunc) MiddlewareFunc {
return func(next HandlerFunc) HandlerFunc {
return func(c Context) error {
if err := h(c); err != nil {
return err
}
return next.Handle(c)
})
return next(c)
}
}
}

func handlerName(h Handler) string {
func handlerName(h HandlerFunc) string {
t := reflect.ValueOf(h).Type()
if t.Kind() == reflect.Func {
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
Expand Down
Loading

0 comments on commit b5d6c05

Please sign in to comment.