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-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
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Server struct {
|
|
|
|
config *engine.Config
|
|
|
|
handler engine.HandlerFunc
|
2016-02-22 05:58:19 +02:00
|
|
|
pool *Pool
|
2016-03-06 19:52:32 +02:00
|
|
|
logger *log.Logger
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-02-22 05:58:19 +02:00
|
|
|
|
|
|
|
Pool struct {
|
|
|
|
request sync.Pool
|
|
|
|
response sync.Pool
|
|
|
|
requestHeader sync.Pool
|
|
|
|
responseHeader 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{
|
|
|
|
config: c,
|
2016-02-22 05:58:19 +02:00
|
|
|
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{}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-02-10 03:16:46 +02:00
|
|
|
handler: func(req engine.Request, res engine.Response) {
|
2016-03-06 19:52:32 +02:00
|
|
|
s.logger.Fatal("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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() {
|
2016-02-23 08:24:56 +02:00
|
|
|
handler := func(c *fasthttp.RequestCtx) {
|
2016-02-22 05:58:19 +02:00
|
|
|
// 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)
|
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
s.handler(req, res)
|
2016-02-22 05:58:19 +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-02-23 08:24:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addr := s.config.Address
|
|
|
|
certfile := s.config.TLSCertfile
|
|
|
|
keyfile := s.config.TLSKeyfile
|
|
|
|
if certfile != "" && keyfile != "" {
|
|
|
|
s.logger.Fatal(fasthttp.ListenAndServeTLS(addr, certfile, keyfile, handler))
|
|
|
|
} else {
|
|
|
|
s.logger.Fatal(fasthttp.ListenAndServe(addr, handler))
|
|
|
|
}
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|