mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
New API Echo#GetContext & Echo#PutContext.
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
e159f4d7ef
commit
3946a1839c
31
echo.go
31
echo.go
@ -187,7 +187,6 @@ var (
|
|||||||
func New() (e *Echo) {
|
func New() (e *Echo) {
|
||||||
e = &Echo{maxParam: new(int)}
|
e = &Echo{maxParam: new(int)}
|
||||||
e.pool.New = func() interface{} {
|
e.pool.New = func() interface{} {
|
||||||
// NOTE: v2
|
|
||||||
return NewContext(nil, nil, e)
|
return NewContext(nil, nil, e)
|
||||||
}
|
}
|
||||||
e.router = NewRouter(e)
|
e.router = NewRouter(e)
|
||||||
@ -198,14 +197,9 @@ func New() (e *Echo) {
|
|||||||
e.pristineHead = e.head
|
e.pristineHead = e.head
|
||||||
e.chainMiddleware()
|
e.chainMiddleware()
|
||||||
|
|
||||||
//----------
|
|
||||||
// Defaults
|
// Defaults
|
||||||
//----------
|
|
||||||
|
|
||||||
e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler)
|
e.SetHTTPErrorHandler(e.DefaultHTTPErrorHandler)
|
||||||
e.SetBinder(&binder{})
|
e.SetBinder(&binder{})
|
||||||
|
|
||||||
// Logger
|
|
||||||
e.logger = log.New("echo")
|
e.logger = log.New("echo")
|
||||||
e.logger.SetLevel(log.FATAL)
|
e.logger.SetLevel(log.FATAL)
|
||||||
|
|
||||||
@ -388,9 +382,6 @@ func (e *Echo) add(method, path string, handler Handler, middleware ...Middlewar
|
|||||||
for i := len(middleware) - 1; i >= 0; i-- {
|
for i := len(middleware) - 1; i >= 0; i-- {
|
||||||
h = middleware[i].Handle(h)
|
h = middleware[i].Handle(h)
|
||||||
}
|
}
|
||||||
// for _, m := range middleware {
|
|
||||||
// h = m.Handle(h)
|
|
||||||
// }
|
|
||||||
return h.Handle(c)
|
return h.Handle(c)
|
||||||
}), e)
|
}), e)
|
||||||
r := Route{
|
r := Route{
|
||||||
@ -443,6 +434,18 @@ func (e *Echo) Routes() []Route {
|
|||||||
return e.router.routes
|
return e.router.routes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetContext returns `Context` from the sync.Pool. You must return the context by
|
||||||
|
// calling `PutContext()`.
|
||||||
|
func (e *Echo) GetContext() Context {
|
||||||
|
return e.pool.Get().(Context)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutContext returns `Context` instance back to the sync.Pool. You must call it after
|
||||||
|
// `GetContext()`.
|
||||||
|
func (e *Echo) PutContext(c Context) {
|
||||||
|
e.pool.Put(c)
|
||||||
|
}
|
||||||
|
|
||||||
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
||||||
c := e.pool.Get().(*context)
|
c := e.pool.Get().(*context)
|
||||||
c.reset(req, res)
|
c.reset(req, res)
|
||||||
@ -455,11 +458,11 @@ func (e *Echo) ServeHTTP(req engine.Request, res engine.Response) {
|
|||||||
e.pool.Put(c)
|
e.pool.Put(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run starts the HTTP engine.
|
// Run starts the HTTP server.
|
||||||
func (e *Echo) Run(eng engine.Engine) {
|
func (e *Echo) Run(s engine.Server) {
|
||||||
eng.SetHandler(e)
|
s.SetHandler(e)
|
||||||
eng.SetLogger(e.logger)
|
s.SetLogger(e.logger)
|
||||||
eng.Start()
|
s.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHTTPError(code int, msg ...string) *HTTPError {
|
func NewHTTPError(code int, msg ...string) *HTTPError {
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Engine defines the interface for HTTP server.
|
// Server defines the interface for HTTP server.
|
||||||
Engine interface {
|
Server interface {
|
||||||
// SetHandler sets the handler for the HTTP server.
|
// SetHandler sets the handler for the HTTP server.
|
||||||
SetHandler(Handler)
|
SetHandler(Handler)
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Server implements `engine.Engine`.
|
// Server implements `engine.Server`.
|
||||||
Server struct {
|
Server struct {
|
||||||
config engine.Config
|
config engine.Config
|
||||||
handler engine.Handler
|
handler engine.Handler
|
||||||
@ -84,17 +84,17 @@ func NewFromConfig(c engine.Config) (s *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHandler implements `engine.Engine#SetHandler` method.
|
// SetHandler implements `engine.Server#SetHandler` method.
|
||||||
func (s *Server) SetHandler(h engine.Handler) {
|
func (s *Server) SetHandler(h engine.Handler) {
|
||||||
s.handler = h
|
s.handler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogger implements `engine.Engine#SetLogger` method.
|
// SetLogger implements `engine.Server#SetLogger` method.
|
||||||
func (s *Server) SetLogger(l *log.Logger) {
|
func (s *Server) SetLogger(l *log.Logger) {
|
||||||
s.logger = l
|
s.logger = l
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start implements `engine.Engine#Start` method.
|
// Start implements `engine.Server#Start` method.
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
handler := func(c *fasthttp.RequestCtx) {
|
handler := func(c *fasthttp.RequestCtx) {
|
||||||
// Request
|
// Request
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
// Server implements `engine.Engine`.
|
// Server implements `engine.Server`.
|
||||||
Server struct {
|
Server struct {
|
||||||
*http.Server
|
*http.Server
|
||||||
config engine.Config
|
config engine.Config
|
||||||
@ -80,17 +80,17 @@ func NewFromConfig(c engine.Config) (s *Server) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHandler implements `engine.Engine#SetHandler` method.
|
// SetHandler implements `engine.Server#SetHandler` method.
|
||||||
func (s *Server) SetHandler(h engine.Handler) {
|
func (s *Server) SetHandler(h engine.Handler) {
|
||||||
s.handler = h
|
s.handler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetLogger implements `engine.Engine#SetLogger` method.
|
// SetLogger implements `engine.Server#SetLogger` method.
|
||||||
func (s *Server) SetLogger(l *log.Logger) {
|
func (s *Server) SetLogger(l *log.Logger) {
|
||||||
s.logger = l
|
s.logger = l
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start implements `engine.Engine#Start` method.
|
// Start implements `engine.Server#Start` method.
|
||||||
func (s *Server) Start() {
|
func (s *Server) Start() {
|
||||||
certfile := s.config.TLSCertfile
|
certfile := s.config.TLSCertfile
|
||||||
keyfile := s.config.TLSKeyfile
|
keyfile := s.config.TLSKeyfile
|
||||||
|
@ -572,6 +572,26 @@ func TestRouterAPI(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkRouterGitHubAPI(b *testing.B) {
|
||||||
|
e := New()
|
||||||
|
r := e.router
|
||||||
|
|
||||||
|
// Add routes
|
||||||
|
for _, route := range api {
|
||||||
|
r.Add(route.Method, route.Path, HandlerFunc(func(c Context) error {
|
||||||
|
return nil
|
||||||
|
}), e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find routes
|
||||||
|
c := e.pool.Get().(*context)
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, route := range api {
|
||||||
|
r.Find(route.Method, route.Path, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (n *node) printTree(pfx string, tail bool) {
|
func (n *node) printTree(pfx string, tail bool) {
|
||||||
p := prefix(tail, pfx, "└── ", "├── ")
|
p := prefix(tail, pfx, "└── ", "├── ")
|
||||||
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.kind, n.parent, n.methodHandler)
|
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.kind, n.parent, n.methodHandler)
|
||||||
|
Loading…
Reference in New Issue
Block a user