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
|
|
|
|
2016-03-07 06:53:23 +02:00
|
|
|
"github.com/labstack/echo"
|
2016-01-29 09:46:11 +02:00
|
|
|
"github.com/labstack/echo/engine"
|
2016-02-10 03:16:46 +02:00
|
|
|
"github.com/labstack/gommon/log"
|
2016-01-29 09:46:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-18 06:49:06 +02:00
|
|
|
// Server implements `engine.Server`.
|
2016-01-29 09:46:11 +02:00
|
|
|
Server struct {
|
2016-03-11 02:35:20 +02:00
|
|
|
*http.Server
|
2016-03-12 13:54:54 +02:00
|
|
|
config engine.Config
|
2016-03-08 18:14:25 +02:00
|
|
|
handler engine.Handler
|
2016-03-06 19:52:32 +02:00
|
|
|
logger *log.Logger
|
2016-03-15 04:58:46 +02:00
|
|
|
pool *pool
|
2016-02-05 00:40:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
pool struct {
|
2016-02-05 00:40:08 +02:00
|
|
|
request sync.Pool
|
|
|
|
response sync.Pool
|
|
|
|
header sync.Pool
|
|
|
|
url sync.Pool
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-03-16 00:55:46 +02:00
|
|
|
// New returns an instance of `standard.Server` with provided listen address.
|
2016-02-10 03:16:46 +02:00
|
|
|
func New(addr string) *Server {
|
2016-03-12 13:54:54 +02:00
|
|
|
c := engine.Config{Address: addr}
|
2016-03-09 06:30:35 +02:00
|
|
|
return NewFromConfig(c)
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// NewFromTLS returns an instance of `standard.Server` from TLS config.
|
2016-03-09 06:30:35 +02:00
|
|
|
func NewFromTLS(addr, certfile, keyfile string) *Server {
|
2016-03-12 13:54:54 +02:00
|
|
|
c := engine.Config{
|
2016-02-09 23:37:38 +02:00
|
|
|
Address: addr,
|
|
|
|
TLSCertfile: certfile,
|
|
|
|
TLSKeyfile: keyfile,
|
|
|
|
}
|
2016-03-09 06:30:35 +02:00
|
|
|
return NewFromConfig(c)
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// NewFromConfig returns an instance of `standard.Server` from config.
|
2016-03-12 13:54:54 +02:00
|
|
|
func NewFromConfig(c engine.Config) (s *Server) {
|
2016-02-10 03:16:46 +02:00
|
|
|
s = &Server{
|
2016-03-11 02:35:20 +02:00
|
|
|
Server: new(http.Server),
|
2016-02-10 03:16:46 +02:00
|
|
|
config: c,
|
2016-03-15 04:58:46 +02:00
|
|
|
pool: &pool{
|
2016-02-05 00:40:08 +02:00
|
|
|
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-03-08 18:14:25 +02:00
|
|
|
handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) {
|
2016-03-18 21:13:37 +02:00
|
|
|
s.logger.Error("handler not set, use `SetHandler()` to set it.")
|
2016-03-08 18:14:25 +02:00
|
|
|
}),
|
2016-02-10 03:16:46 +02:00
|
|
|
logger: log.New("echo"),
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-03-11 02:35:20 +02:00
|
|
|
s.Addr = c.Address
|
|
|
|
s.Handler = s
|
2016-02-10 03:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-18 06:49:06 +02:00
|
|
|
// SetHandler implements `engine.Server#SetHandler` method.
|
2016-03-08 18:14:25 +02:00
|
|
|
func (s *Server) SetHandler(h engine.Handler) {
|
2016-02-10 03:16:46 +02:00
|
|
|
s.handler = h
|
|
|
|
}
|
|
|
|
|
2016-03-18 06:49:06 +02:00
|
|
|
// SetLogger implements `engine.Server#SetLogger` method.
|
2016-03-06 19:52:32 +02:00
|
|
|
func (s *Server) SetLogger(l *log.Logger) {
|
2016-02-10 03:16:46 +02:00
|
|
|
s.logger = l
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 06:49:06 +02:00
|
|
|
// Start implements `engine.Server#Start` method.
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) Start() error {
|
2016-03-18 17:47:03 +02:00
|
|
|
if s.config.Listener == nil {
|
2016-03-18 21:13:37 +02:00
|
|
|
return s.startDefaultListener()
|
2016-03-17 16:15:42 +02:00
|
|
|
}
|
2016-03-18 21:13:37 +02:00
|
|
|
return s.startCustomListener()
|
2016-03-17 16:15:42 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) startDefaultListener() error {
|
|
|
|
c := s.config
|
|
|
|
if c.TLSCertfile != "" && c.TLSKeyfile != "" {
|
|
|
|
return s.ListenAndServeTLS(c.TLSCertfile, c.TLSKeyfile)
|
2016-02-23 08:24:56 +02:00
|
|
|
}
|
2016-03-18 21:13:37 +02:00
|
|
|
return s.ListenAndServe()
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-03-05 06:19:59 +02:00
|
|
|
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) startCustomListener() error {
|
|
|
|
return s.Serve(s.config.Listener)
|
2016-03-17 16:15:42 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
// ServeHTTP implements `http.Handler` interface.
|
2016-03-05 06:19:59 +02:00
|
|
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Request
|
|
|
|
req := 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)
|
|
|
|
req.reset(r, reqHdr, reqURL)
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res := s.pool.response.Get().(*Response)
|
|
|
|
resHdr := s.pool.header.Get().(*Header)
|
|
|
|
resHdr.reset(w.Header())
|
|
|
|
res.reset(w, resHdr)
|
|
|
|
|
2016-03-08 18:14:25 +02:00
|
|
|
s.handler.ServeHTTP(req, res)
|
2016-03-05 06:19:59 +02:00
|
|
|
|
|
|
|
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-03-07 06:53:23 +02:00
|
|
|
|
2016-03-07 08:05:53 +02:00
|
|
|
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
|
|
|
|
func WrapHandler(h http.Handler) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-03-16 01:13:32 +02:00
|
|
|
w := &responseAdapter{
|
|
|
|
ResponseWriter: c.Response().(*Response).ResponseWriter,
|
|
|
|
writer: c.Response(),
|
|
|
|
}
|
2016-03-10 22:05:33 +02:00
|
|
|
r := c.Request().(*Request).Request
|
2016-03-07 06:53:23 +02:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return nil
|
2016-03-07 08:05:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:24:45 +02:00
|
|
|
// WrapMiddleware wraps `func(http.Handler) http.Handler` into `echo.MiddlewareFunc`
|
|
|
|
func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
|
2016-03-07 08:05:53 +02:00
|
|
|
return func(next echo.Handler) echo.Handler {
|
2016-03-10 09:24:45 +02:00
|
|
|
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
2016-03-10 23:29:23 +02:00
|
|
|
req := c.Request().(*Request)
|
|
|
|
res := c.Response().(*Response)
|
2016-03-10 09:24:45 +02:00
|
|
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-03-16 17:24:25 +02:00
|
|
|
res.ResponseWriter = &responseAdapter{
|
|
|
|
ResponseWriter: res.ResponseWriter,
|
|
|
|
writer: c.Response(),
|
|
|
|
}
|
2016-03-10 23:29:23 +02:00
|
|
|
req.Request = r
|
2016-03-10 09:24:45 +02:00
|
|
|
err = next.Handle(c)
|
2016-03-10 23:29:23 +02:00
|
|
|
})).ServeHTTP(res.ResponseWriter, req.Request)
|
2016-03-10 09:24:45 +02:00
|
|
|
return
|
2016-03-07 08:05:53 +02:00
|
|
|
})
|
|
|
|
}
|
2016-03-07 06:53:23 +02:00
|
|
|
}
|