2016-02-09 21:43:00 +02:00
|
|
|
// +build !appengine
|
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
package fasthttp
|
|
|
|
|
|
|
|
import (
|
2016-02-22 05:58:19 +02:00
|
|
|
"sync"
|
2016-02-05 00:40:08 +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-06-01 05:59:52 +02:00
|
|
|
"github.com/labstack/echo/log"
|
|
|
|
glog "github.com/labstack/gommon/log"
|
2016-01-29 09:46:11 +02:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-18 21:13:37 +02:00
|
|
|
*fasthttp.Server
|
2016-03-12 13:54:54 +02:00
|
|
|
config engine.Config
|
2016-03-08 18:14:25 +02:00
|
|
|
handler engine.Handler
|
2016-06-01 05:59:52 +02:00
|
|
|
logger log.Logger
|
2016-03-15 04:58:46 +02:00
|
|
|
pool *pool
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-02-22 05:58:19 +02:00
|
|
|
|
2016-03-15 04:58:46 +02:00
|
|
|
pool struct {
|
2016-02-22 05:58:19 +02:00
|
|
|
request sync.Pool
|
|
|
|
response sync.Pool
|
|
|
|
requestHeader sync.Pool
|
|
|
|
responseHeader sync.Pool
|
|
|
|
url sync.Pool
|
|
|
|
}
|
2016-01-29 09:46:11 +02:00
|
|
|
)
|
|
|
|
|
2016-04-16 18:15:37 +02:00
|
|
|
// New returns `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-04-08 06:20:50 +02:00
|
|
|
return WithConfig(c)
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-16 18:15:37 +02:00
|
|
|
// WithTLS returns `Server` with provided TLS config.
|
2016-04-25 19:58:11 +02:00
|
|
|
func WithTLS(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,
|
2016-06-16 23:25:32 +02:00
|
|
|
TLSCertFile: certFile,
|
|
|
|
TLSKeyFile: keyFile,
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
2016-04-08 06:20:50 +02:00
|
|
|
return WithConfig(c)
|
2016-02-09 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-04-16 18:15:37 +02:00
|
|
|
// WithConfig returns `Server` with provided config.
|
2016-04-08 06:20:50 +02:00
|
|
|
func WithConfig(c engine.Config) (s *Server) {
|
2016-02-10 03:16:46 +02:00
|
|
|
s = &Server{
|
2016-03-18 21:13:37 +02:00
|
|
|
Server: new(fasthttp.Server),
|
2016-02-10 03:16:46 +02:00
|
|
|
config: c,
|
2016-03-15 04:58:46 +02:00
|
|
|
pool: &pool{
|
2016-02-22 05:58:19 +02:00
|
|
|
request: sync.Pool{
|
|
|
|
New: func() interface{} {
|
2016-04-16 18:15:37 +02:00
|
|
|
return &Request{logger: s.logger}
|
2016-02-22 05:58:19 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
response: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &Response{logger: s.logger}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
requestHeader: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &RequestHeader{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
responseHeader: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return &ResponseHeader{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
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-06-01 05:59:52 +02:00
|
|
|
logger: glog.New("echo"),
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-06-16 03:05:48 +02:00
|
|
|
s.ReadTimeout = c.ReadTimeout
|
|
|
|
s.WriteTimeout = c.WriteTimeout
|
2016-03-18 21:13:37 +02:00
|
|
|
s.Handler = s.ServeHTTP
|
2016-02-10 03:16:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// SetHandler implements `engine.Server#SetHandler` function.
|
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-20 00:47:20 +02:00
|
|
|
// SetLogger implements `engine.Server#SetLogger` function.
|
2016-06-01 05:59:52 +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-20 00:47:20 +02:00
|
|
|
// Start implements `engine.Server#Start` function.
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) Start() error {
|
|
|
|
if s.config.Listener == nil {
|
|
|
|
return s.startDefaultListener()
|
2016-02-23 08:24:56 +02:00
|
|
|
}
|
2016-03-18 21:13:37 +02:00
|
|
|
return s.startCustomListener()
|
2016-02-23 08:24:56 +02:00
|
|
|
|
2016-03-18 21:13:37 +02:00
|
|
|
}
|
2016-03-17 16:15:42 +02:00
|
|
|
|
2016-09-12 15:18:58 +02:00
|
|
|
// Stop implements `engine.Server#Stop` function.
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
// TODO: implement `engine.Server#Stop` function
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) startDefaultListener() error {
|
|
|
|
c := s.config
|
2016-06-16 23:25:32 +02:00
|
|
|
if c.TLSCertFile != "" && c.TLSKeyFile != "" {
|
|
|
|
return s.ListenAndServeTLS(c.Address, c.TLSCertFile, c.TLSKeyFile)
|
2016-03-17 16:15:42 +02:00
|
|
|
}
|
2016-03-18 21:13:37 +02:00
|
|
|
return s.ListenAndServe(c.Address)
|
2016-03-17 16:15:42 +02:00
|
|
|
}
|
|
|
|
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) startCustomListener() error {
|
|
|
|
c := s.config
|
2016-06-16 23:25:32 +02:00
|
|
|
if c.TLSCertFile != "" && c.TLSKeyFile != "" {
|
|
|
|
return s.ServeTLS(c.Listener, c.TLSCertFile, c.TLSKeyFile)
|
2016-02-23 08:24:56 +02:00
|
|
|
}
|
2016-03-18 21:13:37 +02:00
|
|
|
return s.Serve(c.Listener)
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-03-07 06:53:23 +02:00
|
|
|
|
2016-03-18 21:13:37 +02:00
|
|
|
func (s *Server) ServeHTTP(c *fasthttp.RequestCtx) {
|
|
|
|
// Request
|
2016-04-24 19:21:23 +02:00
|
|
|
req := s.pool.request.Get().(*Request)
|
|
|
|
reqHdr := s.pool.requestHeader.Get().(*RequestHeader)
|
|
|
|
reqURL := s.pool.url.Get().(*URL)
|
|
|
|
reqHdr.reset(&c.Request.Header)
|
|
|
|
reqURL.reset(c.URI())
|
|
|
|
req.reset(c, reqHdr, reqURL)
|
2016-03-18 21:13:37 +02:00
|
|
|
|
|
|
|
// Response
|
2016-04-24 19:21:23 +02:00
|
|
|
res := s.pool.response.Get().(*Response)
|
|
|
|
resHdr := s.pool.responseHeader.Get().(*ResponseHeader)
|
|
|
|
resHdr.reset(&c.Response.Header)
|
|
|
|
res.reset(c, resHdr)
|
2016-03-18 21:13:37 +02:00
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
s.handler.ServeHTTP(req, res)
|
2016-03-18 21:13:37 +02:00
|
|
|
|
2016-04-01 17:51:18 +02:00
|
|
|
// Return to pool
|
2016-04-24 19:21:23 +02:00
|
|
|
s.pool.request.Put(req)
|
|
|
|
s.pool.requestHeader.Put(reqHdr)
|
|
|
|
s.pool.url.Put(reqURL)
|
|
|
|
s.pool.response.Put(res)
|
|
|
|
s.pool.responseHeader.Put(resHdr)
|
2016-03-17 16:15:42 +02:00
|
|
|
}
|
|
|
|
|
2016-03-07 08:05:53 +02:00
|
|
|
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
|
|
|
|
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-04-24 19:21:23 +02:00
|
|
|
req := c.Request().(*Request)
|
|
|
|
res := c.Response().(*Response)
|
|
|
|
ctx := req.RequestCtx
|
2016-03-07 06:53:23 +02:00
|
|
|
h(ctx)
|
2016-04-24 19:21:23 +02:00
|
|
|
res.status = ctx.Response.StatusCode()
|
|
|
|
res.size = int64(ctx.Response.Header.ContentLength())
|
2016-03-07 06:53:23 +02:00
|
|
|
return nil
|
2016-03-07 08:05:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 02:08:13 +02:00
|
|
|
// WrapMiddleware wraps `func(fasthttp.RequestHandler) fasthttp.RequestHandler`
|
|
|
|
// into `echo.MiddlewareFunc`
|
|
|
|
func WrapMiddleware(m func(fasthttp.RequestHandler) fasthttp.RequestHandler) echo.MiddlewareFunc {
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
2016-06-08 02:08:13 +02:00
|
|
|
return func(c echo.Context) (err error) {
|
2016-04-24 19:21:23 +02:00
|
|
|
req := c.Request().(*Request)
|
|
|
|
res := c.Response().(*Response)
|
|
|
|
ctx := req.RequestCtx
|
2016-06-08 02:08:13 +02:00
|
|
|
m(func(ctx *fasthttp.RequestCtx) {
|
|
|
|
next(c)
|
|
|
|
})(ctx)
|
2016-04-24 19:21:23 +02:00
|
|
|
res.status = ctx.Response.StatusCode()
|
|
|
|
res.size = int64(ctx.Response.Header.ContentLength())
|
2016-06-08 02:08:13 +02:00
|
|
|
return
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-03-07 08:05:53 +02:00
|
|
|
}
|
2016-03-07 06:53:23 +02:00
|
|
|
}
|