forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
180 lines (160 loc) · 4.2 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// +build !appengine
package fasthttp
import (
"sync"
"github.com/labstack/echo"
"github.com/labstack/echo/engine"
"github.com/labstack/echo/log"
glog "github.com/labstack/gommon/log"
"github.com/valyala/fasthttp"
)
type (
// Server implements `engine.Server`.
Server struct {
*fasthttp.Server
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 `Server` with provided listen address.
func New(addr string) *Server {
c := engine.Config{Address: addr}
return WithConfig(c)
}
// WithTLS returns `Server` with provided TLS config.
func WithTLS(addr, certFile, keyFile string) *Server {
c := engine.Config{
Address: addr,
TLSCertfile: certFile,
TLSKeyfile: keyFile,
}
return WithConfig(c)
}
// WithConfig returns `Server` with provided config.
func WithConfig(c engine.Config) (s *Server) {
s = &Server{
Server: new(fasthttp.Server),
config: c,
pool: &pool{
request: sync.Pool{
New: func() interface{} {
return &Request{logger: s.logger}
},
},
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: glog.New("echo"),
}
s.Handler = s.ServeHTTP
return
}
// SetHandler implements `engine.Server#SetHandler` function.
func (s *Server) SetHandler(h engine.Handler) {
s.handler = h
}
// SetLogger implements `engine.Server#SetLogger` function.
func (s *Server) SetLogger(l log.Logger) {
s.logger = l
}
// Start implements `engine.Server#Start` function.
func (s *Server) Start() error {
if s.config.Listener == nil {
return s.startDefaultListener()
}
return s.startCustomListener()
}
func (s *Server) startDefaultListener() error {
c := s.config
if c.TLSCertfile != "" && c.TLSKeyfile != "" {
return s.ListenAndServeTLS(c.Address, c.TLSCertfile, c.TLSKeyfile)
}
return s.ListenAndServe(c.Address)
}
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)
// Return to pool
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)
}
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request().(*Request)
res := c.Response().(*Response)
ctx := req.RequestCtx
h(ctx)
res.status = ctx.Response.StatusCode()
res.size = int64(ctx.Response.Header.ContentLength())
return nil
}
}
// WrapMiddleware wraps `func(fasthttp.RequestHandler) fasthttp.RequestHandler`
// into `echo.MiddlewareFunc`
func WrapMiddleware(m func(fasthttp.RequestHandler) fasthttp.RequestHandler) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
req := c.Request().(*Request)
res := c.Response().(*Response)
ctx := req.RequestCtx
m(func(ctx *fasthttp.RequestCtx) {
next(c)
})(ctx)
res.status = ctx.Response.StatusCode()
res.size = int64(ctx.Response.Header.ContentLength())
return
}
}
}