mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Dropped Engine**#Object functions
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
38d010090f
commit
8ee864ae08
@ -26,6 +26,7 @@ type (
|
||||
Context interface {
|
||||
netContext.Context
|
||||
Request() engine.Request
|
||||
SetResponse(engine.Response)
|
||||
Response() engine.Response
|
||||
Path() string
|
||||
P(int) string
|
||||
@ -110,7 +111,12 @@ func (c *context) Request() engine.Request {
|
||||
return c.request
|
||||
}
|
||||
|
||||
// Response returns *Response.
|
||||
// SetResponse sets `engine.Response`.
|
||||
func (c *context) SetResponse(r engine.Response) {
|
||||
c.response = r
|
||||
}
|
||||
|
||||
// Response returns `engine.Response`.
|
||||
func (c *context) Response() engine.Response {
|
||||
return c.response
|
||||
}
|
||||
|
@ -30,7 +30,6 @@ type (
|
||||
Method() string
|
||||
Body() io.ReadCloser
|
||||
FormValue(string) string
|
||||
Object() interface{}
|
||||
}
|
||||
|
||||
// Response defines an interface for HTTP response.
|
||||
@ -41,9 +40,8 @@ type (
|
||||
Status() int
|
||||
Size() int64
|
||||
Committed() bool
|
||||
SetWriter(io.Writer)
|
||||
Writer() io.Writer
|
||||
Object() interface{}
|
||||
// SetWriter(io.Writer)
|
||||
// Writer() io.Writer
|
||||
}
|
||||
|
||||
// Header defines an interface for HTTP header.
|
||||
@ -52,7 +50,6 @@ type (
|
||||
Del(string)
|
||||
Get(string) string
|
||||
Set(string, string)
|
||||
Object() interface{}
|
||||
}
|
||||
|
||||
// URL defines an interface for HTTP request url.
|
||||
@ -60,7 +57,6 @@ type (
|
||||
SetPath(string)
|
||||
Path() string
|
||||
QueryValue(string) string
|
||||
Object() interface{}
|
||||
}
|
||||
|
||||
// Config defines engine configuration.
|
||||
|
@ -6,11 +6,11 @@ import "github.com/valyala/fasthttp"
|
||||
|
||||
type (
|
||||
RequestHeader struct {
|
||||
header fasthttp.RequestHeader
|
||||
fasthttp.RequestHeader
|
||||
}
|
||||
|
||||
ResponseHeader struct {
|
||||
header fasthttp.ResponseHeader
|
||||
fasthttp.ResponseHeader
|
||||
}
|
||||
)
|
||||
|
||||
@ -19,23 +19,19 @@ func (h *RequestHeader) Add(key, val string) {
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Del(key string) {
|
||||
h.header.Del(key)
|
||||
h.RequestHeader.Del(key)
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Get(key string) string {
|
||||
return string(h.header.Peek(key))
|
||||
return string(h.Peek(key))
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Set(key, val string) {
|
||||
h.header.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Object() interface{} {
|
||||
return h.header
|
||||
h.RequestHeader.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *RequestHeader) reset(hdr fasthttp.RequestHeader) {
|
||||
h.header = hdr
|
||||
h.RequestHeader = hdr
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Add(key, val string) {
|
||||
@ -43,21 +39,17 @@ func (h *ResponseHeader) Add(key, val string) {
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Del(key string) {
|
||||
h.header.Del(key)
|
||||
h.ResponseHeader.Del(key)
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Get(key string) string {
|
||||
return string(h.header.Peek(key))
|
||||
return string(h.Peek(key))
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Set(key, val string) {
|
||||
h.header.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Object() interface{} {
|
||||
return h.header
|
||||
h.ResponseHeader.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) reset(hdr fasthttp.ResponseHeader) {
|
||||
h.header = hdr
|
||||
h.ResponseHeader = hdr
|
||||
}
|
||||
|
@ -15,34 +15,34 @@ import (
|
||||
|
||||
type (
|
||||
Request struct {
|
||||
context *fasthttp.RequestCtx
|
||||
url engine.URL
|
||||
header engine.Header
|
||||
*fasthttp.RequestCtx
|
||||
url engine.URL
|
||||
header engine.Header
|
||||
}
|
||||
)
|
||||
|
||||
func NewRequest(c *fasthttp.RequestCtx) *Request {
|
||||
return &Request{
|
||||
context: c,
|
||||
url: &URL{url: c.URI()},
|
||||
header: &RequestHeader{c.Request.Header},
|
||||
RequestCtx: c,
|
||||
url: &URL{URI: c.URI()},
|
||||
header: &RequestHeader{c.Request.Header},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) TLS() bool {
|
||||
return r.context.IsTLS()
|
||||
return r.IsTLS()
|
||||
}
|
||||
|
||||
func (r *Request) Scheme() string {
|
||||
return string(r.context.URI().Scheme())
|
||||
return string(r.RequestCtx.URI().Scheme())
|
||||
}
|
||||
|
||||
func (r *Request) Host() string {
|
||||
return string(r.context.Host())
|
||||
return string(r.RequestCtx.Host())
|
||||
}
|
||||
|
||||
func (r *Request) URI() string {
|
||||
return string(r.context.RequestURI())
|
||||
return string(r.RequestURI())
|
||||
}
|
||||
|
||||
func (r *Request) URL() engine.URL {
|
||||
@ -54,27 +54,23 @@ func (r *Request) Header() engine.Header {
|
||||
}
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
return r.context.RemoteAddr().String()
|
||||
return r.RemoteAddr().String()
|
||||
}
|
||||
|
||||
func (r *Request) Method() string {
|
||||
return string(r.context.Method())
|
||||
return string(r.RequestCtx.Method())
|
||||
}
|
||||
|
||||
func (r *Request) Body() io.ReadCloser {
|
||||
return ioutil.NopCloser(bytes.NewBuffer(r.context.PostBody()))
|
||||
return ioutil.NopCloser(bytes.NewBuffer(r.PostBody()))
|
||||
}
|
||||
|
||||
func (r *Request) FormValue(name string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (r *Request) Object() interface{} {
|
||||
return r.context
|
||||
}
|
||||
|
||||
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
|
||||
r.context = c
|
||||
r.RequestCtx = c
|
||||
r.header = h
|
||||
r.url = u
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
|
||||
type (
|
||||
Response struct {
|
||||
context *fasthttp.RequestCtx
|
||||
*fasthttp.RequestCtx
|
||||
header engine.Header
|
||||
status int
|
||||
size int64
|
||||
@ -25,17 +25,13 @@ type (
|
||||
|
||||
func NewResponse(c *fasthttp.RequestCtx) *Response {
|
||||
return &Response{
|
||||
context: c,
|
||||
header: &ResponseHeader{c.Response.Header},
|
||||
writer: c,
|
||||
logger: log.New("test"),
|
||||
RequestCtx: c,
|
||||
header: &ResponseHeader{c.Response.Header},
|
||||
writer: c,
|
||||
logger: log.New("test"),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Response) Object() interface{} {
|
||||
return r.context
|
||||
}
|
||||
|
||||
func (r *Response) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
@ -46,12 +42,12 @@ func (r *Response) WriteHeader(code int) {
|
||||
return
|
||||
}
|
||||
r.status = code
|
||||
r.context.SetStatusCode(code)
|
||||
r.SetStatusCode(code)
|
||||
r.committed = true
|
||||
}
|
||||
|
||||
func (r *Response) Write(b []byte) (int, error) {
|
||||
return r.context.Write(b)
|
||||
return r.RequestCtx.Write(b)
|
||||
}
|
||||
|
||||
func (r *Response) Status() int {
|
||||
@ -66,16 +62,16 @@ func (r *Response) Committed() bool {
|
||||
return r.committed
|
||||
}
|
||||
|
||||
func (r *Response) SetWriter(w io.Writer) {
|
||||
r.writer = w
|
||||
}
|
||||
|
||||
func (r *Response) Writer() io.Writer {
|
||||
return r.writer
|
||||
}
|
||||
// func (r *Response) SetWriter(w io.Writer) {
|
||||
// r.writer = w
|
||||
// }
|
||||
//
|
||||
// func (r *Response) Writer() io.Writer {
|
||||
// return r.writer
|
||||
// }
|
||||
|
||||
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
|
||||
r.context = c
|
||||
r.RequestCtx = c
|
||||
r.header = h
|
||||
r.status = http.StatusOK
|
||||
r.size = 0
|
||||
|
@ -126,7 +126,7 @@ func (s *Server) Start() {
|
||||
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
|
||||
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
ctx := c.Request().Object().(*fasthttp.RequestCtx)
|
||||
ctx := c.Request().(*Request).RequestCtx
|
||||
h(ctx)
|
||||
return nil
|
||||
}
|
||||
@ -136,7 +136,7 @@ func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
||||
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().Object().(*fasthttp.RequestCtx)
|
||||
ctx := c.Request().(*Request).RequestCtx
|
||||
h(ctx)
|
||||
return next.Handle(c)
|
||||
})
|
||||
|
@ -6,7 +6,7 @@ import "github.com/valyala/fasthttp"
|
||||
|
||||
type (
|
||||
URL struct {
|
||||
url *fasthttp.URI
|
||||
*fasthttp.URI
|
||||
}
|
||||
)
|
||||
|
||||
@ -15,17 +15,13 @@ func (u *URL) SetPath(path string) {
|
||||
}
|
||||
|
||||
func (u *URL) Path() string {
|
||||
return string(u.url.Path())
|
||||
return string(u.URI.Path())
|
||||
}
|
||||
|
||||
func (u *URL) QueryValue(name string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (u *URL) Object() interface{} {
|
||||
return u.url
|
||||
}
|
||||
|
||||
func (u *URL) reset(url *fasthttp.URI) {
|
||||
u.url = url
|
||||
func (u *URL) reset(uri *fasthttp.URI) {
|
||||
u.URI = uri
|
||||
}
|
||||
|
@ -4,30 +4,26 @@ import "net/http"
|
||||
|
||||
type (
|
||||
Header struct {
|
||||
header http.Header
|
||||
http.Header
|
||||
}
|
||||
)
|
||||
|
||||
func (h *Header) Add(key, val string) {
|
||||
h.header.Add(key, val)
|
||||
h.Header.Add(key, val)
|
||||
}
|
||||
|
||||
func (h *Header) Del(key string) {
|
||||
h.header.Del(key)
|
||||
h.Header.Del(key)
|
||||
}
|
||||
|
||||
func (h *Header) Get(key string) string {
|
||||
return h.header.Get(key)
|
||||
return h.Header.Get(key)
|
||||
}
|
||||
|
||||
func (h *Header) Set(key, val string) {
|
||||
h.header.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *Header) Object() interface{} {
|
||||
return h.header
|
||||
h.Header.Set(key, val)
|
||||
}
|
||||
|
||||
func (h *Header) reset(hdr http.Header) {
|
||||
h.header = hdr
|
||||
h.Header = hdr
|
||||
}
|
||||
|
@ -9,22 +9,22 @@ import (
|
||||
|
||||
type (
|
||||
Request struct {
|
||||
request *http.Request
|
||||
url engine.URL
|
||||
header engine.Header
|
||||
*http.Request
|
||||
url engine.URL
|
||||
header engine.Header
|
||||
}
|
||||
)
|
||||
|
||||
func NewRequest(r *http.Request) *Request {
|
||||
return &Request{
|
||||
request: r,
|
||||
url: &URL{url: r.URL},
|
||||
Request: r,
|
||||
url: &URL{URL: r.URL},
|
||||
header: &Header{r.Header},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Request) TLS() bool {
|
||||
return r.request.TLS != nil
|
||||
return r.Request.TLS != nil
|
||||
}
|
||||
|
||||
func (r *Request) Scheme() string {
|
||||
@ -35,7 +35,7 @@ func (r *Request) Scheme() string {
|
||||
}
|
||||
|
||||
func (r *Request) Host() string {
|
||||
return r.request.Host
|
||||
return r.Request.Host
|
||||
}
|
||||
|
||||
func (r *Request) URL() engine.URL {
|
||||
@ -59,31 +59,27 @@ func (r *Request) Header() engine.Header {
|
||||
// }
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
return r.request.RemoteAddr
|
||||
return r.RemoteAddr
|
||||
}
|
||||
|
||||
func (r *Request) Method() string {
|
||||
return r.request.Method
|
||||
return r.Request.Method
|
||||
}
|
||||
|
||||
func (r *Request) URI() string {
|
||||
return r.request.RequestURI
|
||||
return r.RequestURI
|
||||
}
|
||||
|
||||
func (r *Request) Body() io.ReadCloser {
|
||||
return r.request.Body
|
||||
return r.Request.Body
|
||||
}
|
||||
|
||||
func (r *Request) FormValue(name string) string {
|
||||
return r.request.FormValue(name)
|
||||
}
|
||||
|
||||
func (r *Request) Object() interface{} {
|
||||
return r.request
|
||||
return r.Request.FormValue(name)
|
||||
}
|
||||
|
||||
func (r *Request) reset(req *http.Request, h engine.Header, u engine.URL) {
|
||||
r.request = req
|
||||
r.Request = req
|
||||
r.header = h
|
||||
r.url = u
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package standard
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/engine"
|
||||
@ -10,22 +9,22 @@ import (
|
||||
|
||||
type (
|
||||
Response struct {
|
||||
response http.ResponseWriter
|
||||
http.ResponseWriter
|
||||
header engine.Header
|
||||
status int
|
||||
size int64
|
||||
committed bool
|
||||
writer io.Writer
|
||||
logger *log.Logger
|
||||
// writer io.Writer
|
||||
logger *log.Logger
|
||||
}
|
||||
)
|
||||
|
||||
func NewResponse(w http.ResponseWriter, l *log.Logger) *Response {
|
||||
return &Response{
|
||||
response: w,
|
||||
header: &Header{w.Header()},
|
||||
writer: w,
|
||||
logger: l,
|
||||
ResponseWriter: w,
|
||||
header: &Header{w.Header()},
|
||||
// writer: w,
|
||||
logger: l,
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,12 +38,12 @@ func (r *Response) WriteHeader(code int) {
|
||||
return
|
||||
}
|
||||
r.status = code
|
||||
r.response.WriteHeader(code)
|
||||
r.ResponseWriter.WriteHeader(code)
|
||||
r.committed = true
|
||||
}
|
||||
|
||||
func (r *Response) Write(b []byte) (n int, err error) {
|
||||
n, err = r.writer.Write(b)
|
||||
n, err = r.ResponseWriter.Write(b)
|
||||
r.size += int64(n)
|
||||
return
|
||||
}
|
||||
@ -61,23 +60,19 @@ func (r *Response) Committed() bool {
|
||||
return r.committed
|
||||
}
|
||||
|
||||
func (r *Response) SetWriter(w io.Writer) {
|
||||
r.writer = w
|
||||
}
|
||||
// func (r *Response) SetWriter(w io.Writer) {
|
||||
// r.writer = w
|
||||
// }
|
||||
|
||||
func (r *Response) Writer() io.Writer {
|
||||
return r.writer
|
||||
}
|
||||
|
||||
func (r *Response) Object() interface{} {
|
||||
return r.response
|
||||
}
|
||||
// func (r *Response) Writer() io.Writer {
|
||||
// return r.writer
|
||||
// }
|
||||
|
||||
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
||||
r.response = w
|
||||
r.ResponseWriter = w
|
||||
r.header = h
|
||||
r.status = http.StatusOK
|
||||
r.size = 0
|
||||
r.committed = false
|
||||
r.writer = w
|
||||
// r.writer = w
|
||||
}
|
||||
|
@ -125,8 +125,8 @@ func (s *Server) Server() *http.Server {
|
||||
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
|
||||
func WrapHandler(h http.Handler) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
w := c.Response().Object().(http.ResponseWriter)
|
||||
r := c.Request().Object().(*http.Request)
|
||||
w := c.Response().(*Response).ResponseWriter
|
||||
r := c.Request().(*Request).Request
|
||||
h.ServeHTTP(w, r)
|
||||
return nil
|
||||
}
|
||||
@ -136,8 +136,8 @@ func WrapHandler(h http.Handler) echo.HandlerFunc {
|
||||
func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
|
||||
return func(next echo.Handler) echo.Handler {
|
||||
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
||||
w := c.Response().Object().(http.ResponseWriter)
|
||||
r := c.Request().Object().(*http.Request)
|
||||
w := c.Response().(*Response).ResponseWriter
|
||||
r := c.Request().(*Request).Request
|
||||
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
err = next.Handle(c)
|
||||
})).ServeHTTP(w, r)
|
||||
|
@ -4,34 +4,26 @@ import "net/url"
|
||||
|
||||
type (
|
||||
URL struct {
|
||||
url *url.URL
|
||||
*url.URL
|
||||
query url.Values
|
||||
}
|
||||
)
|
||||
|
||||
func (u *URL) URL() *url.URL {
|
||||
return u.url
|
||||
}
|
||||
|
||||
func (u *URL) SetPath(path string) {
|
||||
u.url.Path = path
|
||||
u.URL.Path = path
|
||||
}
|
||||
|
||||
func (u *URL) Path() string {
|
||||
return u.url.Path
|
||||
return u.URL.Path
|
||||
}
|
||||
|
||||
func (u *URL) QueryValue(name string) string {
|
||||
if u.query == nil {
|
||||
u.query = u.url.Query()
|
||||
u.query = u.Query()
|
||||
}
|
||||
return u.query.Get(name)
|
||||
}
|
||||
|
||||
func (u *URL) Object() interface{} {
|
||||
return u.url
|
||||
}
|
||||
|
||||
func (u *URL) reset(url *url.URL) {
|
||||
u.url = url
|
||||
u.URL = url
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -19,9 +17,9 @@ type (
|
||||
level int
|
||||
}
|
||||
|
||||
gzipWriter struct {
|
||||
io.Writer
|
||||
gzipResponseWriter struct {
|
||||
engine.Response
|
||||
io.Writer
|
||||
}
|
||||
)
|
||||
|
||||
@ -33,15 +31,15 @@ func Gzip(options ...*GzipOptions) echo.MiddlewareFunc {
|
||||
return echo.HandlerFunc(func(c echo.Context) error {
|
||||
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
||||
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
||||
w := writerPool.Get().(*gzip.Writer)
|
||||
w.Reset(c.Response().Writer())
|
||||
w := pool.Get().(*gzip.Writer)
|
||||
w.Reset(c.Response())
|
||||
defer func() {
|
||||
w.Close()
|
||||
writerPool.Put(w)
|
||||
pool.Put(w)
|
||||
}()
|
||||
gw := gzipWriter{Writer: w, Response: c.Response()}
|
||||
gw := gzipResponseWriter{Response: c.Response(), Writer: w}
|
||||
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
||||
c.Response().SetWriter(gw)
|
||||
c.SetResponse(gw)
|
||||
}
|
||||
if err := next.Handle(c); err != nil {
|
||||
c.Error(err)
|
||||
@ -51,26 +49,14 @@ func Gzip(options ...*GzipOptions) echo.MiddlewareFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func (w gzipWriter) Write(b []byte) (int, error) {
|
||||
if w.Header().Get(echo.ContentType) == "" {
|
||||
w.Header().Set(echo.ContentType, http.DetectContentType(b))
|
||||
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
||||
if g.Header().Get(echo.ContentType) == "" {
|
||||
g.Header().Set(echo.ContentType, http.DetectContentType(b))
|
||||
}
|
||||
return w.Writer.Write(b)
|
||||
return g.Writer.Write(b)
|
||||
}
|
||||
|
||||
func (w gzipWriter) Flush() error {
|
||||
return w.Writer.(*gzip.Writer).Flush()
|
||||
}
|
||||
|
||||
func (w gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
return w.Response.(http.Hijacker).Hijack()
|
||||
}
|
||||
|
||||
func (w *gzipWriter) CloseNotify() <-chan bool {
|
||||
return w.Response.(http.CloseNotifier).CloseNotify()
|
||||
}
|
||||
|
||||
var writerPool = sync.Pool{
|
||||
var pool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return gzip.NewWriter(ioutil.Discard)
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user