2016-03-07 06:53:23 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/engine"
|
|
|
|
"github.com/labstack/gommon/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Server struct {
|
|
|
|
*http.Server
|
|
|
|
config *engine.Config
|
2016-03-08 18:14:25 +02:00
|
|
|
handler engine.Handler
|
2016-03-07 06:53:23 +02:00
|
|
|
pool *Pool
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
Pool struct {
|
|
|
|
request sync.Pool
|
|
|
|
response sync.Pool
|
|
|
|
header sync.Pool
|
|
|
|
url sync.Pool
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func New(addr string) *Server {
|
|
|
|
c := &engine.Config{Address: addr}
|
|
|
|
return NewConfig(c)
|
|
|
|
}
|
|
|
|
|
2016-04-25 19:58:11 +02:00
|
|
|
func NewTLS(addr, certFile, keyFile string) *Server {
|
2016-03-07 06:53:23 +02:00
|
|
|
c := &engine.Config{
|
|
|
|
Address: addr,
|
2016-06-16 23:25:32 +02:00
|
|
|
TLSCertFile: certFile,
|
|
|
|
TLSKeyFile: keyFile,
|
2016-03-07 06:53:23 +02:00
|
|
|
}
|
|
|
|
return NewConfig(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(c *engine.Config) (s *Server) {
|
|
|
|
s = &Server{
|
|
|
|
Server: new(http.Server),
|
|
|
|
config: c,
|
|
|
|
pool: &Pool{
|
|
|
|
request: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Request{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
response: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Response{logger: s.logger}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
header: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Header{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
url: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &URL{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-04-24 19:21:23 +02:00
|
|
|
handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) {
|
2016-08-18 20:27:37 +02:00
|
|
|
panic("echo: handler not set, use `Server#SetHandler()` to set it.")
|
2016-03-08 18:14:25 +02:00
|
|
|
}),
|
2016-03-07 06:53:23 +02:00
|
|
|
logger: log.New("echo"),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-08 18:14:25 +02:00
|
|
|
func (s *Server) SetHandler(h engine.Handler) {
|
2016-03-07 06:53:23 +02:00
|
|
|
s.handler = h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetLogger(l *log.Logger) {
|
|
|
|
s.logger = l
|
|
|
|
}
|
|
|
|
|
2016-08-18 20:27:37 +02:00
|
|
|
func (s *Server) Start() error {
|
|
|
|
if s.config.Listener == nil {
|
|
|
|
return s.startDefaultListener()
|
2016-03-07 06:53:23 +02:00
|
|
|
}
|
2016-08-18 20:27:37 +02:00
|
|
|
return s.startCustomListener()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startDefaultListener() error {
|
|
|
|
c := s.config
|
|
|
|
if c.TLSCertFile != "" && c.TLSKeyFile != "" {
|
|
|
|
return s.ListenAndServeTLS(c.TLSCertFile, c.TLSKeyFile)
|
|
|
|
}
|
|
|
|
return s.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startCustomListener() error {
|
|
|
|
return s.Serve(s.config.Listener)
|
2016-03-07 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Request
|
2016-04-24 19:21:23 +02:00
|
|
|
req := s.pool.request.Get().(*Request)
|
2016-03-07 06:53:23 +02:00
|
|
|
reqHdr := s.pool.header.Get().(*Header)
|
|
|
|
reqURL := s.pool.url.Get().(*URL)
|
|
|
|
reqHdr.reset(r.Header)
|
|
|
|
reqURL.reset(r.URL)
|
2016-04-24 19:21:23 +02:00
|
|
|
req.reset(r, reqHdr, reqURL)
|
2016-03-07 06:53:23 +02:00
|
|
|
|
|
|
|
// Response
|
2016-04-24 19:21:23 +02:00
|
|
|
res := s.pool.response.Get().(*Response)
|
2016-03-07 06:53:23 +02:00
|
|
|
resHdr := s.pool.header.Get().(*Header)
|
|
|
|
resHdr.reset(w.Header())
|
2016-04-24 19:21:23 +02:00
|
|
|
res.reset(w, resHdr)
|
2016-03-07 06:53:23 +02:00
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
s.handler.ServeHTTP(req, res)
|
2016-03-07 06:53:23 +02:00
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
s.pool.request.Put(req)
|
2016-03-07 06:53:23 +02:00
|
|
|
s.pool.header.Put(reqHdr)
|
|
|
|
s.pool.url.Put(reqURL)
|
2016-04-24 19:21:23 +02:00
|
|
|
s.pool.response.Put(res)
|
2016-03-07 06:53:23 +02:00
|
|
|
s.pool.header.Put(resHdr)
|
|
|
|
}
|