2016-01-29 09:46:11 +02:00
|
|
|
package standard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-02-05 00:40:08 +02:00
|
|
|
"sync"
|
2016-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo/engine"
|
2016-02-10 03:16:46 +02:00
|
|
|
"github.com/labstack/echo/logger"
|
|
|
|
"github.com/labstack/gommon/log"
|
2016-01-29 09:46:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Server struct {
|
|
|
|
*http.Server
|
|
|
|
config *engine.Config
|
|
|
|
handler engine.HandlerFunc
|
2016-02-05 00:40:08 +02:00
|
|
|
pool *Pool
|
2016-02-10 03:16:46 +02:00
|
|
|
logger logger.Logger
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Pool struct {
|
|
|
|
request sync.Pool
|
|
|
|
response sync.Pool
|
|
|
|
header sync.Pool
|
|
|
|
url sync.Pool
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-10 03:16:46 +02:00
|
|
|
func New(addr string) *Server {
|
2016-02-09 23:37:38 +02:00
|
|
|
c := &engine.Config{Address: addr}
|
2016-02-10 03:16:46 +02:00
|
|
|
return NewConfig(c)
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 03:16:46 +02:00
|
|
|
func NewTLS(addr, certfile, keyfile string) *Server {
|
2016-02-09 23:37:38 +02:00
|
|
|
c := &engine.Config{
|
|
|
|
Address: addr,
|
|
|
|
TLSCertfile: certfile,
|
|
|
|
TLSKeyfile: keyfile,
|
|
|
|
}
|
2016-02-10 03:16:46 +02:00
|
|
|
return NewConfig(c)
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-02-10 03:16:46 +02:00
|
|
|
func NewConfig(c *engine.Config) (s *Server) {
|
|
|
|
s = &Server{
|
|
|
|
Server: new(http.Server),
|
|
|
|
config: c,
|
2016-02-05 00:40:08 +02:00
|
|
|
pool: &Pool{
|
|
|
|
request: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Request{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
response: sync.Pool{
|
|
|
|
New: func() interface{} {
|
2016-02-10 03:16:46 +02:00
|
|
|
return &Response{logger: s.logger}
|
2016-02-05 00:40:08 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
header: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Header{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
url: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &URL{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-02-10 03:16:46 +02:00
|
|
|
handler: func(req engine.Request, res engine.Response) {
|
2016-02-22 05:58:19 +02:00
|
|
|
s.logger.Warn("handler not set")
|
2016-02-10 03:16:46 +02:00
|
|
|
},
|
|
|
|
logger: log.New("echo"),
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-02-10 03:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetHandler(h engine.HandlerFunc) {
|
|
|
|
s.handler = h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) SetLogger(l logger.Logger) {
|
|
|
|
s.logger = l
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() {
|
|
|
|
s.Addr = s.config.Address
|
|
|
|
s.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-02-05 00:40:08 +02:00
|
|
|
// Request
|
|
|
|
req := s.pool.request.Get().(*Request)
|
2016-02-09 03:26:00 +02:00
|
|
|
reqHdr := s.pool.header.Get().(*Header)
|
|
|
|
reqURL := s.pool.url.Get().(*URL)
|
2016-02-05 00:40:08 +02:00
|
|
|
reqHdr.reset(r.Header)
|
|
|
|
reqURL.reset(r.URL)
|
|
|
|
req.reset(r, reqHdr, reqURL)
|
|
|
|
|
|
|
|
// Response
|
2016-02-09 03:26:00 +02:00
|
|
|
res := s.pool.response.Get().(*Response)
|
|
|
|
resHdr := s.pool.header.Get().(*Header)
|
2016-02-17 21:58:03 +02:00
|
|
|
resHdr.reset(w.Header())
|
|
|
|
res.reset(w, resHdr)
|
2016-02-05 00:40:08 +02:00
|
|
|
|
|
|
|
s.handler(req, res)
|
|
|
|
|
|
|
|
s.pool.request.Put(req)
|
|
|
|
s.pool.header.Put(reqHdr)
|
|
|
|
s.pool.url.Put(reqURL)
|
|
|
|
s.pool.response.Put(res)
|
|
|
|
s.pool.header.Put(resHdr)
|
2016-01-29 09:46:11 +02:00
|
|
|
})
|
2016-02-05 05:29:49 +02:00
|
|
|
s.logger.Fatal(s.ListenAndServe())
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|