1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00
echo/test/server.go
Vishal Rana a66162a3d2 Logging middleware fixes
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-03-21 17:27:14 -07:00

119 lines
2.2 KiB
Go

package test
import (
"net/http"
"sync"
"github.com/labstack/echo/engine"
"github.com/labstack/gommon/log"
)
type (
Server struct {
*http.Server
config *engine.Config
handler engine.Handler
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)
}
func NewTLS(addr, certfile, keyfile string) *Server {
c := &engine.Config{
Address: addr,
TLSCertfile: certfile,
TLSKeyfile: keyfile,
}
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{}
},
},
},
handler: engine.HandlerFunc(func(rq engine.Request, rs engine.Response) {
s.logger.Fatal("handler not set")
}),
logger: log.New("echo"),
}
return
}
func (s *Server) SetHandler(h engine.Handler) {
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
certfile := s.config.TLSCertfile
keyfile := s.config.TLSKeyfile
if certfile != "" && keyfile != "" {
s.logger.Fatal(s.ListenAndServeTLS(certfile, keyfile))
} else {
s.logger.Fatal(s.ListenAndServe())
}
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Request
rq := s.pool.request.Get().(*Request)
reqHdr := s.pool.header.Get().(*Header)
reqURL := s.pool.url.Get().(*URL)
reqHdr.reset(r.Header)
reqURL.reset(r.URL)
rq.reset(r, reqHdr, reqURL)
// Response
rs := s.pool.response.Get().(*Response)
resHdr := s.pool.header.Get().(*Header)
resHdr.reset(w.Header())
rs.reset(w, resHdr)
s.handler.ServeHTTP(rq, rs)
s.pool.request.Put(rq)
s.pool.header.Put(reqHdr)
s.pool.url.Put(reqURL)
s.pool.response.Put(rs)
s.pool.header.Put(resHdr)
}