diff --git a/context.go b/context.go index 093fdcb3..da2086a6 100644 --- a/context.go +++ b/context.go @@ -18,7 +18,6 @@ import ( "bytes" netContext "golang.org/x/net/context" - "golang.org/x/net/websocket" ) type ( @@ -28,7 +27,6 @@ type ( netContext.Context Request() engine.Request Response() engine.Response - Socket() *websocket.Conn Path() string P(int) string Param(string) string @@ -59,7 +57,6 @@ type ( context struct { request engine.Request response engine.Response - socket *websocket.Conn path string pnames []string pvalues []string @@ -118,11 +115,6 @@ func (c *context) Response() engine.Response { return c.response } -// Socket returns *websocket.Conn. -func (c *context) Socket() *websocket.Conn { - return c.socket -} - // Path returns the registered path for the handler. func (c *context) Path() string { return c.path diff --git a/context_test.go b/context_test.go index 25df7e8d..83f8672b 100644 --- a/context_test.go +++ b/context_test.go @@ -43,9 +43,6 @@ func TestContext(t *testing.T) { // Response assert.NotNil(t, c.Response()) - // Socket - assert.Nil(t, c.Socket()) - // ParamNames c.Object().pnames = []string{"uid", "fid"} assert.EqualValues(t, []string{"uid", "fid"}, c.ParamNames()) diff --git a/echo.go b/echo.go index a5ec5f00..0edb3ace 100644 --- a/echo.go +++ b/echo.go @@ -426,7 +426,7 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) { // Run starts the HTTP engine. func (e *Echo) Run(eng engine.Engine) { - eng.SetHandler(e.ServeHTTP) + eng.SetHandler(e) eng.SetLogger(e.logger) eng.Start() } diff --git a/engine/engine.go b/engine/engine.go index 4f0c2e09..97558356 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -8,10 +8,8 @@ import ( ) type ( - HandlerFunc func(Request, Response) - Engine interface { - SetHandler(HandlerFunc) + SetHandler(Handler) SetLogger(*log.Logger) Start() } @@ -67,4 +65,14 @@ type ( ReadTimeout time.Duration WriteTimeout time.Duration } + + Handler interface { + ServeHTTP(Request, Response) + } + + HandlerFunc func(Request, Response) ) + +func (h HandlerFunc) ServeHTTP(req Request, res Response) { + h.ServeHTTP(req, res) +} diff --git a/engine/fasthttp/server.go b/engine/fasthttp/server.go index a6ffcbb2..8c069ddd 100644 --- a/engine/fasthttp/server.go +++ b/engine/fasthttp/server.go @@ -14,7 +14,7 @@ import ( type ( Server struct { config *engine.Config - handler engine.HandlerFunc + handler engine.Handler pool *Pool logger *log.Logger } @@ -72,15 +72,15 @@ func NewConfig(c *engine.Config) (s *Server) { }, }, }, - handler: func(req engine.Request, res engine.Response) { + handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) { s.logger.Fatal("handler not set") - }, + }), logger: log.New("echo"), } return } -func (s *Server) SetHandler(h engine.HandlerFunc) { +func (s *Server) SetHandler(h engine.Handler) { s.handler = h } @@ -104,7 +104,7 @@ func (s *Server) Start() { resHdr.reset(c.Response.Header) res.reset(c, resHdr) - s.handler(req, res) + s.handler.ServeHTTP(req, res) s.pool.request.Put(req) s.pool.requestHeader.Put(reqHdr) diff --git a/engine/standard/server.go b/engine/standard/server.go index 1f195e6f..1acb1f6b 100644 --- a/engine/standard/server.go +++ b/engine/standard/server.go @@ -13,7 +13,7 @@ type ( Server struct { *http.Server config *engine.Config - handler engine.HandlerFunc + handler engine.Handler pool *Pool logger *log.Logger } @@ -66,15 +66,15 @@ func NewConfig(c *engine.Config) (s *Server) { }, }, }, - handler: func(req engine.Request, res engine.Response) { + handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) { s.logger.Fatal("handler not set") - }, + }), logger: log.New("echo"), } return } -func (s *Server) SetHandler(h engine.HandlerFunc) { +func (s *Server) SetHandler(h engine.Handler) { s.handler = h } @@ -109,7 +109,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { resHdr.reset(w.Header()) res.reset(w, resHdr) - s.handler(req, res) + s.handler.ServeHTTP(req, res) s.pool.request.Put(req) s.pool.header.Put(reqHdr) diff --git a/test/server.go b/test/server.go index 66261968..444cb37a 100644 --- a/test/server.go +++ b/test/server.go @@ -12,7 +12,7 @@ type ( Server struct { *http.Server config *engine.Config - handler engine.HandlerFunc + handler engine.Handler pool *Pool logger *log.Logger } @@ -65,15 +65,15 @@ func NewConfig(c *engine.Config) (s *Server) { }, }, }, - handler: func(req engine.Request, res engine.Response) { + handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) { s.logger.Fatal("handler not set") - }, + }), logger: log.New("echo"), } return } -func (s *Server) SetHandler(h engine.HandlerFunc) { +func (s *Server) SetHandler(h engine.Handler) { s.handler = h } @@ -108,7 +108,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { resHdr.reset(w.Header()) res.reset(w, resHdr) - s.handler(req, res) + s.handler.ServeHTTP(req, res) s.pool.request.Put(req) s.pool.header.Put(reqHdr) diff --git a/website/content/guide/migrating.md b/website/content/guide/migrating.md index 4fa0bc9d..c9860539 100644 --- a/website/content/guide/migrating.md +++ b/website/content/guide/migrating.md @@ -8,11 +8,11 @@ menu: ### Migrating from v1 -#### What got changed? +#### Change log - Echo now uses `Engine` interface to abstract `HTTP` server implementation, allowing -us to use HTTP servers beyond Go standard library, supports standard HTTP server and [FastHTTP](https://github.com/valyala/fasthttp). -- Context, Request and Response as an interface, enabling adding your own functions and easy testing. [More...](https://github.com/labstack/echo/issues/146) +us to use HTTP servers beyond the standard library. It currently supports standard HTTP server and [FastHTTP](https://github.com/valyala/fasthttp). +- Context, Request and Response are converted to interfaces. [More...](https://github.com/labstack/echo/issues/146) - Moved API's for serving static files into middleware. - `Echo#Index` - `Echo#Favicon` @@ -29,35 +29,3 @@ it can be achieved via middleware. - Ability to define middleware at route level. #### How? - -##### v1 Handler - -```go -func welcome(c *echo.Context) error { - return c.String(http.StatusOK, "Welcome!\n") -} -``` - -##### v2 Handler - -```go -func welcome(echo.HandlerFunc(c echo.Context) error { - return c.String(http.StatusOK, "Welcome!\n") -}) -``` - -##### v1 Middleware - -```go -func welcome(c *echo.Context) error { - return c.String(http.StatusOK, "Welcome!\n") -} -``` - -v2 - -```go -func welcome(echo.HandlerFunc(c echo.Context) error { - return c.String(http.StatusOK, "Welcome!\n") -}) -```