1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Removed Context#Socket

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-08 08:14:25 -08:00
parent b6deec4897
commit f4a5abc8b2
8 changed files with 30 additions and 65 deletions

View File

@ -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

View File

@ -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())

View File

@ -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()
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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")
})
```