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-04-25 19:58:11 +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-03-07 06:53:23 +02:00
|
|
|
s.logger.Fatal("handler not set")
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() {
|
|
|
|
s.Addr = s.config.Address
|
|
|
|
s.Handler = s
|
2016-04-25 19:58:11 +02:00
|
|
|
certFile := s.config.TLSCertfile
|
|
|
|
keyFile := s.config.TLSKeyfile
|
|
|
|
if certFile != "" && keyFile != "" {
|
|
|
|
s.logger.Fatal(s.ListenAndServeTLS(certFile, keyFile))
|
2016-03-07 06:53:23 +02:00
|
|
|
} else {
|
|
|
|
s.logger.Fatal(s.ListenAndServe())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|