1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-22 20:06:21 +02:00
echo/engine/fasthttp/server.go

168 lines
3.8 KiB
Go
Raw Normal View History

// +build !appengine
package fasthttp
import (
"sync"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
type (
// Server implements `engine.Server`.
Server struct {
*fasthttp.Server
2016-03-12 13:54:54 +02:00
config engine.Config
handler engine.Handler
logger *log.Logger
pool *pool
}
pool struct {
request sync.Pool
response sync.Pool
requestHeader sync.Pool
responseHeader sync.Pool
url sync.Pool
}
)
// New returns an instance of `fasthttp.Server` with provided listen address.
func New(addr string) *Server {
2016-03-12 13:54:54 +02:00
c := engine.Config{Address: addr}
return NewFromConfig(c)
}
// NewFromTLS returns an instance of `fasthttp.Server` from TLS config.
func NewFromTLS(addr, certfile, keyfile string) *Server {
2016-03-12 13:54:54 +02:00
c := engine.Config{
Address: addr,
TLSCertfile: certfile,
TLSKeyfile: keyfile,
}
return NewFromConfig(c)
}
// NewFromConfig returns an instance of `standard.Server` from config.
2016-03-12 13:54:54 +02:00
func NewFromConfig(c engine.Config) (s *Server) {
s = &Server{
Server: new(fasthttp.Server),
config: c,
pool: &pool{
request: sync.Pool{
New: func() interface{} {
return &Request{}
},
},
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{}
},
},
},
handler: engine.HandlerFunc(func(req engine.Request, res engine.Response) {
s.logger.Error("handler not set, use `SetHandler()` to set it.")
}),
logger: log.New("echo"),
}
s.Handler = s.ServeHTTP
return
}
// SetHandler implements `engine.Server#SetHandler` method.
func (s *Server) SetHandler(h engine.Handler) {
s.handler = h
}
// SetLogger implements `engine.Server#SetLogger` method.
func (s *Server) SetLogger(l *log.Logger) {
s.logger = l
}
// Start implements `engine.Server#Start` method.
func (s *Server) Start() error {
if s.config.Listener == nil {
return s.startDefaultListener()
}
return s.startCustomListener()
}
2016-03-17 16:15:42 +02:00
func (s *Server) startDefaultListener() error {
c := s.config
if c.TLSCertfile != "" && c.TLSKeyfile != "" {
return s.ListenAndServeTLS(c.Address, c.TLSCertfile, c.TLSKeyfile)
2016-03-17 16:15:42 +02:00
}
return s.ListenAndServe(c.Address)
2016-03-17 16:15:42 +02:00
}
func (s *Server) startCustomListener() error {
c := s.config
if c.TLSCertfile != "" && c.TLSKeyfile != "" {
return s.ServeTLS(c.Listener, c.TLSCertfile, c.TLSKeyfile)
}
return s.Serve(c.Listener)
}
func (s *Server) ServeHTTP(c *fasthttp.RequestCtx) {
// Request
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)
// Response
res := s.pool.response.Get().(*Response)
resHdr := s.pool.responseHeader.Get().(*ResponseHeader)
resHdr.reset(&c.Response.Header)
res.reset(c, resHdr)
s.handler.ServeHTTP(req, res)
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
}
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().(*Request).RequestCtx
h(ctx)
return nil
}
}
// WrapMiddleware wraps `fasthttp.RequestHandler` into `echo.MiddlewareFunc`
func WrapMiddleware(h fasthttp.RequestHandler) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
ctx := c.Request().(*Request).RequestCtx
h(ctx)
return next.Handle(c)
})
}
}