mirror of
https://github.com/labstack/echo.git
synced 2025-05-13 22:06:36 +02:00
Revert "Fixed Request.Out
"
This reverts commit cced590a168b62cd5a5bdb155db2a064ff7a6054.
This commit is contained in:
parent
cced590a16
commit
72050d765b
@ -1,6 +1,3 @@
|
|||||||
|
|
||||||
# **WORK IN PROGRESS!**
|
|
||||||
|
|
||||||
# [Echo](http://labstack.com/echo) [](http://godoc.org/github.com/labstack/echo) [](https://raw.githubusercontent.com/labstack/echo/master/LICENSE) [](https://travis-ci.org/labstack/echo) [](https://coveralls.io/r/labstack/echo) [](https://gitter.im/labstack/echo)
|
# [Echo](http://labstack.com/echo) [](http://godoc.org/github.com/labstack/echo) [](https://raw.githubusercontent.com/labstack/echo/master/LICENSE) [](https://travis-ci.org/labstack/echo) [](https://coveralls.io/r/labstack/echo) [](https://gitter.im/labstack/echo)
|
||||||
|
|
||||||
A fast and unfancy micro web framework for Go.
|
A fast and unfancy micro web framework for Go.
|
||||||
|
37
context.go
37
context.go
@ -7,14 +7,13 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/labstack/echo/engine"
|
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
netContext "golang.org/x/net/context"
|
xcontext "golang.org/x/net/context"
|
||||||
"golang.org/x/net/websocket"
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,9 +21,9 @@ type (
|
|||||||
// Context represents context for the current request. It holds request and
|
// Context represents context for the current request. It holds request and
|
||||||
// response objects, path parameters, data and registered handler.
|
// response objects, path parameters, data and registered handler.
|
||||||
Context interface {
|
Context interface {
|
||||||
netContext.Context
|
xcontext.Context
|
||||||
Request() engine.Request
|
Request() *http.Request
|
||||||
Response() engine.Response
|
Response() *Response
|
||||||
Socket() *websocket.Conn
|
Socket() *websocket.Conn
|
||||||
Path() string
|
Path() string
|
||||||
P(int) string
|
P(int) string
|
||||||
@ -51,8 +50,8 @@ type (
|
|||||||
}
|
}
|
||||||
|
|
||||||
context struct {
|
context struct {
|
||||||
request engine.Request
|
request *http.Request
|
||||||
response engine.Response
|
response *Response
|
||||||
socket *websocket.Conn
|
socket *websocket.Conn
|
||||||
path string
|
path string
|
||||||
pnames []string
|
pnames []string
|
||||||
@ -66,7 +65,7 @@ type (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewContext creates a Context object.
|
// NewContext creates a Context object.
|
||||||
func NewContext(req engine.Request, res engine.Response, e *Echo) Context {
|
func NewContext(req *http.Request, res *Response, e *Echo) Context {
|
||||||
return &context{
|
return &context{
|
||||||
request: req,
|
request: req,
|
||||||
response: res,
|
response: res,
|
||||||
@ -93,12 +92,12 @@ func (c *context) Value(key interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Request returns *http.Request.
|
// Request returns *http.Request.
|
||||||
func (c *context) Request() engine.Request {
|
func (c *context) Request() *http.Request {
|
||||||
return c.request
|
return c.request
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response returns *Response.
|
// Response returns *Response.
|
||||||
func (c *context) Response() engine.Response {
|
func (c *context) Response() *Response {
|
||||||
return c.response
|
return c.response
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,17 +135,14 @@ func (c *context) Param(name string) (value string) {
|
|||||||
// Query returns query parameter by name.
|
// Query returns query parameter by name.
|
||||||
func (c *context) Query(name string) string {
|
func (c *context) Query(name string) string {
|
||||||
if c.query == nil {
|
if c.query == nil {
|
||||||
// TODO: v2
|
c.query = c.request.URL.Query()
|
||||||
// c.query = c.request.URL.Query()
|
|
||||||
}
|
}
|
||||||
return c.query.Get(name)
|
return c.query.Get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Form returns form parameter by name.
|
// Form returns form parameter by name.
|
||||||
func (c *context) Form(name string) string {
|
func (c *context) Form(name string) string {
|
||||||
// TODO: v2
|
return c.request.FormValue(name)
|
||||||
// return c.request.FormValue(name)
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retrieves data from the context.
|
// Get retrieves data from the context.
|
||||||
@ -298,8 +294,7 @@ func (c *context) Redirect(code int, url string) error {
|
|||||||
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
||||||
return InvalidRedirectCode
|
return InvalidRedirectCode
|
||||||
}
|
}
|
||||||
// TODO: v2
|
http.Redirect(c.response, c.request, url, code)
|
||||||
// http.Redirect(c.response, c.request, url, code)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,11 +313,9 @@ func (c *context) X() *context {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *context) reset(req engine.Request, res engine.Response, e *Echo) {
|
func (c *context) reset(r *http.Request, w http.ResponseWriter, e *Echo) {
|
||||||
c.request = req
|
c.request = r
|
||||||
// TODO: v2
|
c.response.reset(w, e)
|
||||||
// c.response.reset(res, e)
|
|
||||||
c.response = res
|
|
||||||
c.query = nil
|
c.query = nil
|
||||||
c.store = nil
|
c.store = nil
|
||||||
c.echo = e
|
c.echo = e
|
||||||
|
316
echo.go
316
echo.go
@ -16,11 +16,9 @@ import (
|
|||||||
|
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
|
||||||
"github.com/labstack/echo/engine"
|
|
||||||
"github.com/labstack/echo/engine/fasthttp"
|
|
||||||
"github.com/labstack/echo/engine/standard"
|
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -36,10 +34,8 @@ type (
|
|||||||
renderer Renderer
|
renderer Renderer
|
||||||
pool sync.Pool
|
pool sync.Pool
|
||||||
debug bool
|
debug bool
|
||||||
hook engine.HandlerFunc
|
hook http.HandlerFunc
|
||||||
autoIndex bool
|
autoIndex bool
|
||||||
engineType engine.Type
|
|
||||||
engine engine.Engine
|
|
||||||
router *Router
|
router *Router
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
@ -47,7 +43,7 @@ type (
|
|||||||
Route struct {
|
Route struct {
|
||||||
Method string
|
Method string
|
||||||
Path string
|
Path string
|
||||||
Handler string
|
Handler Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTPError struct {
|
HTTPError struct {
|
||||||
@ -55,9 +51,9 @@ type (
|
|||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Middleware interface{}
|
Middleware interface{}
|
||||||
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
MiddlewareFunc func(HandlerFunc) HandlerFunc
|
||||||
// Handler interface{}
|
Handler interface{}
|
||||||
HandlerFunc func(Context) error
|
HandlerFunc func(Context) error
|
||||||
|
|
||||||
// HTTPErrorHandler is a centralized HTTP error handler.
|
// HTTPErrorHandler is a centralized HTTP error handler.
|
||||||
@ -65,7 +61,7 @@ type (
|
|||||||
|
|
||||||
// Binder is the interface that wraps the Bind method.
|
// Binder is the interface that wraps the Bind method.
|
||||||
Binder interface {
|
Binder interface {
|
||||||
Bind(engine.Request, interface{}) error
|
Bind(*http.Request, interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
binder struct {
|
binder struct {
|
||||||
@ -192,8 +188,7 @@ var (
|
|||||||
func New() (e *Echo) {
|
func New() (e *Echo) {
|
||||||
e = &Echo{maxParam: new(int)}
|
e = &Echo{maxParam: new(int)}
|
||||||
e.pool.New = func() interface{} {
|
e.pool.New = func() interface{} {
|
||||||
// NOTE: v2
|
return NewContext(nil, new(Response), e)
|
||||||
return NewContext(nil, nil, e)
|
|
||||||
}
|
}
|
||||||
e.router = NewRouter(e)
|
e.router = NewRouter(e)
|
||||||
|
|
||||||
@ -203,21 +198,20 @@ func New() (e *Echo) {
|
|||||||
|
|
||||||
e.HTTP2(true)
|
e.HTTP2(true)
|
||||||
e.defaultHTTPErrorHandler = func(err error, c Context) {
|
e.defaultHTTPErrorHandler = func(err error, c Context) {
|
||||||
// TODO: v2
|
x := c.X()
|
||||||
// x := c.X()
|
code := http.StatusInternalServerError
|
||||||
// code := http.StatusInternalServerError
|
msg := http.StatusText(code)
|
||||||
// msg := http.StatusText(code)
|
if he, ok := err.(*HTTPError); ok {
|
||||||
// if he, ok := err.(*HTTPError); ok {
|
code = he.code
|
||||||
// code = he.code
|
msg = he.message
|
||||||
// msg = he.message
|
}
|
||||||
// }
|
if e.debug {
|
||||||
// if e.debug {
|
msg = err.Error()
|
||||||
// msg = err.Error()
|
}
|
||||||
// }
|
if !x.response.committed {
|
||||||
// if !x.response.Committed() {
|
http.Error(x.response, msg, code)
|
||||||
// http.Error(x.response, msg, code)
|
}
|
||||||
// }
|
e.logger.Error(err)
|
||||||
// e.logger.Error(err)
|
|
||||||
}
|
}
|
||||||
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
|
e.SetHTTPErrorHandler(e.defaultHTTPErrorHandler)
|
||||||
e.SetBinder(&binder{})
|
e.SetBinder(&binder{})
|
||||||
@ -297,80 +291,95 @@ func (e *Echo) AutoIndex(on bool) {
|
|||||||
// Hook registers a callback which is invoked from `Echo#ServerHTTP` as the first
|
// Hook registers a callback which is invoked from `Echo#ServerHTTP` as the first
|
||||||
// statement. Hook is useful if you want to modify response/response objects even
|
// statement. Hook is useful if you want to modify response/response objects even
|
||||||
// before it hits the router or any middleware.
|
// before it hits the router or any middleware.
|
||||||
func (e *Echo) Hook(h engine.HandlerFunc) {
|
func (e *Echo) Hook(h http.HandlerFunc) {
|
||||||
e.hook = h
|
e.hook = h
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use adds handler to the middleware chain.
|
// Use adds handler to the middleware chain.
|
||||||
func (e *Echo) Use(m ...MiddlewareFunc) {
|
func (e *Echo) Use(m ...Middleware) {
|
||||||
for _, h := range m {
|
for _, h := range m {
|
||||||
e.middleware = append(e.middleware, h)
|
e.middleware = append(e.middleware, wrapMiddleware(h))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect adds a CONNECT route > handler to the router.
|
// Connect adds a CONNECT route > handler to the router.
|
||||||
func (e *Echo) Connect(path string, h HandlerFunc) {
|
func (e *Echo) Connect(path string, h Handler) {
|
||||||
e.add(CONNECT, path, h)
|
e.add(CONNECT, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete adds a DELETE route > handler to the router.
|
// Delete adds a DELETE route > handler to the router.
|
||||||
func (e *Echo) Delete(path string, h HandlerFunc) {
|
func (e *Echo) Delete(path string, h Handler) {
|
||||||
e.add(DELETE, path, h)
|
e.add(DELETE, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get adds a GET route > handler to the router.
|
// Get adds a GET route > handler to the router.
|
||||||
func (e *Echo) Get(path string, h HandlerFunc) {
|
func (e *Echo) Get(path string, h Handler) {
|
||||||
e.add(GET, path, h)
|
e.add(GET, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Head adds a HEAD route > handler to the router.
|
// Head adds a HEAD route > handler to the router.
|
||||||
func (e *Echo) Head(path string, h HandlerFunc) {
|
func (e *Echo) Head(path string, h Handler) {
|
||||||
e.add(HEAD, path, h)
|
e.add(HEAD, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options adds an OPTIONS route > handler to the router.
|
// Options adds an OPTIONS route > handler to the router.
|
||||||
func (e *Echo) Options(path string, h HandlerFunc) {
|
func (e *Echo) Options(path string, h Handler) {
|
||||||
e.add(OPTIONS, path, h)
|
e.add(OPTIONS, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Patch adds a PATCH route > handler to the router.
|
// Patch adds a PATCH route > handler to the router.
|
||||||
func (e *Echo) Patch(path string, h HandlerFunc) {
|
func (e *Echo) Patch(path string, h Handler) {
|
||||||
e.add(PATCH, path, h)
|
e.add(PATCH, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post adds a POST route > handler to the router.
|
// Post adds a POST route > handler to the router.
|
||||||
func (e *Echo) Post(path string, h HandlerFunc) {
|
func (e *Echo) Post(path string, h Handler) {
|
||||||
e.add(POST, path, h)
|
e.add(POST, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put adds a PUT route > handler to the router.
|
// Put adds a PUT route > handler to the router.
|
||||||
func (e *Echo) Put(path string, h HandlerFunc) {
|
func (e *Echo) Put(path string, h Handler) {
|
||||||
e.add(PUT, path, h)
|
e.add(PUT, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace adds a TRACE route > handler to the router.
|
// Trace adds a TRACE route > handler to the router.
|
||||||
func (e *Echo) Trace(path string, h HandlerFunc) {
|
func (e *Echo) Trace(path string, h Handler) {
|
||||||
e.add(TRACE, path, h)
|
e.add(TRACE, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Any adds a route > handler to the router for all HTTP methods.
|
// Any adds a route > handler to the router for all HTTP methods.
|
||||||
func (e *Echo) Any(path string, h HandlerFunc) {
|
func (e *Echo) Any(path string, h Handler) {
|
||||||
for _, m := range methods {
|
for _, m := range methods {
|
||||||
e.add(m, path, h)
|
e.add(m, path, h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Match adds a route > handler to the router for multiple HTTP methods provided.
|
// Match adds a route > handler to the router for multiple HTTP methods provided.
|
||||||
func (e *Echo) Match(methods []string, path string, h HandlerFunc) {
|
func (e *Echo) Match(methods []string, path string, h Handler) {
|
||||||
for _, m := range methods {
|
for _, m := range methods {
|
||||||
e.add(m, path, h)
|
e.add(m, path, h)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: v2
|
// WebSocket adds a WebSocket route > handler to the router.
|
||||||
func (e *Echo) add(method, path string, h HandlerFunc) {
|
func (e *Echo) WebSocket(path string, h HandlerFunc) {
|
||||||
|
e.Get(path, func(c Context) (err error) {
|
||||||
|
x := c.X()
|
||||||
|
wss := websocket.Server{
|
||||||
|
Handler: func(ws *websocket.Conn) {
|
||||||
|
x.socket = ws
|
||||||
|
x.response.status = http.StatusSwitchingProtocols
|
||||||
|
err = h(c)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
wss.ServeHTTP(x.response, x.request)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Echo) add(method, path string, h Handler) {
|
||||||
path = e.prefix + path
|
path = e.prefix + path
|
||||||
e.router.Add(method, path, h, e)
|
e.router.Add(method, path, wrapHandler(h), e)
|
||||||
r := Route{
|
r := Route{
|
||||||
Method: method,
|
Method: method,
|
||||||
Path: path,
|
Path: path,
|
||||||
@ -410,36 +419,36 @@ func (e *Echo) ServeFile(path, file string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Echo) serveFile(dir, file string, c Context) (err error) {
|
func (e *Echo) serveFile(dir, file string, c Context) (err error) {
|
||||||
// TODO: v2
|
x := c.X()
|
||||||
// x := c.X()
|
fs := http.Dir(dir)
|
||||||
// fs := http.Dir(dir)
|
f, err := fs.Open(file)
|
||||||
// f, err := fs.Open(file)
|
if err != nil {
|
||||||
// if err != nil {
|
return NewHTTPError(http.StatusNotFound)
|
||||||
// return NewHTTPError(http.StatusNotFound)
|
}
|
||||||
// }
|
defer f.Close()
|
||||||
// defer f.Close()
|
|
||||||
//
|
fi, _ := f.Stat()
|
||||||
// fi, _ := f.Stat()
|
if fi.IsDir() {
|
||||||
// if fi.IsDir() {
|
/* NOTE:
|
||||||
// /* NOTE:
|
Not checking the Last-Modified header as it caches the response `304` when
|
||||||
// Not checking the Last-Modified header as it caches the response `304` when
|
changing differnt directories for the same path.
|
||||||
// changing differnt directories for the same path.
|
*/
|
||||||
// */
|
d := f
|
||||||
// d := f
|
|
||||||
//
|
// Index file
|
||||||
// // Index file
|
file = filepath.Join(file, indexPage)
|
||||||
// file = filepath.Join(file, indexPage)
|
f, err = fs.Open(file)
|
||||||
// f, err = fs.Open(file)
|
if err != nil {
|
||||||
// if err != nil {
|
if e.autoIndex {
|
||||||
// if e.autoIndex {
|
// Auto index
|
||||||
// // Auto index
|
return listDir(d, c)
|
||||||
// return listDir(d, c)
|
}
|
||||||
// }
|
return NewHTTPError(http.StatusForbidden)
|
||||||
// return NewHTTPError(http.StatusForbidden)
|
}
|
||||||
// }
|
fi, _ = f.Stat() // Index file stat
|
||||||
// fi, _ = f.Stat() // Index file stat
|
}
|
||||||
// }
|
|
||||||
// http.ServeContent(x.response, x.request, fi.Name(), fi.ModTime(), f)
|
http.ServeContent(x.response, x.request, fi.Name(), fi.ModTime(), f)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -468,7 +477,7 @@ func listDir(d http.File, c Context) (err error) {
|
|||||||
|
|
||||||
// Group creates a new sub router with prefix. It inherits all properties from
|
// Group creates a new sub router with prefix. It inherits all properties from
|
||||||
// the parent. Passing middleware overrides parent middleware.
|
// the parent. Passing middleware overrides parent middleware.
|
||||||
func (e *Echo) Group(prefix string, m ...MiddlewareFunc) *Group {
|
func (e *Echo) Group(prefix string, m ...Middleware) *Group {
|
||||||
g := &Group{*e}
|
g := &Group{*e}
|
||||||
g.echo.prefix += prefix
|
g.echo.prefix += prefix
|
||||||
if len(m) == 0 {
|
if len(m) == 0 {
|
||||||
@ -483,7 +492,7 @@ func (e *Echo) Group(prefix string, m ...MiddlewareFunc) *Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// URI generates a URI from handler.
|
// URI generates a URI from handler.
|
||||||
func (e *Echo) URI(h HandlerFunc, params ...interface{}) string {
|
func (e *Echo) URI(h Handler, params ...interface{}) string {
|
||||||
uri := new(bytes.Buffer)
|
uri := new(bytes.Buffer)
|
||||||
pl := len(params)
|
pl := len(params)
|
||||||
n := 0
|
n := 0
|
||||||
@ -508,7 +517,7 @@ func (e *Echo) URI(h HandlerFunc, params ...interface{}) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// URL is an alias for `URI` function.
|
// URL is an alias for `URI` function.
|
||||||
func (e *Echo) URL(h HandlerFunc, params ...interface{}) string {
|
func (e *Echo) URL(h Handler, params ...interface{}) string {
|
||||||
return e.URI(h, params...)
|
return e.URI(h, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -519,53 +528,13 @@ func (e *Echo) Routes() []Route {
|
|||||||
|
|
||||||
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
||||||
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
// TODO: v2
|
|
||||||
// if e.hook != nil {
|
|
||||||
// e.hook(w, r)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// c := e.pool.Get().(*context)
|
|
||||||
// h, e := e.router.Find(r.Method, r.URL.Path, c)
|
|
||||||
// c.reset(r, w, e)
|
|
||||||
//
|
|
||||||
// // Chain middleware with handler in the end
|
|
||||||
// for i := len(e.middleware) - 1; i >= 0; i-- {
|
|
||||||
// h = e.middleware[i](h)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Execute chain
|
|
||||||
// if err := h(c); err != nil {
|
|
||||||
// e.httpErrorHandler(err, c)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// e.pool.Put(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server returns the internal *http.Server.
|
|
||||||
func (e *Echo) Server(addr string) *http.Server {
|
|
||||||
s := &http.Server{Addr: addr, Handler: e}
|
|
||||||
// TODO: Remove in Go 1.6+
|
|
||||||
if e.http2 {
|
|
||||||
http2.ConfigureServer(s, nil)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Echo) SetEngine(t engine.Type) {
|
|
||||||
e.engineType = t
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run runs a server.
|
|
||||||
func (e *Echo) Run(address string) {
|
|
||||||
config := &engine.Config{Address: address}
|
|
||||||
handler := func(req engine.Request, res engine.Response) {
|
|
||||||
if e.hook != nil {
|
if e.hook != nil {
|
||||||
e.hook(req, res)
|
e.hook(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
c := e.pool.Get().(*context)
|
c := e.pool.Get().(*context)
|
||||||
h, e := e.router.Find(req.Method(), req.URL().Path(), c)
|
h, e := e.router.Find(r.Method, r.URL.Path, c)
|
||||||
c.reset(req, res, e)
|
c.reset(r, w, e)
|
||||||
|
|
||||||
// Chain middleware with handler in the end
|
// Chain middleware with handler in the end
|
||||||
for i := len(e.middleware) - 1; i >= 0; i-- {
|
for i := len(e.middleware) - 1; i >= 0; i-- {
|
||||||
@ -579,16 +548,20 @@ func (e *Echo) Run(address string) {
|
|||||||
|
|
||||||
e.pool.Put(c)
|
e.pool.Put(c)
|
||||||
}
|
}
|
||||||
e.engine = standard.NewServer(config, handler)
|
|
||||||
|
|
||||||
switch e.engineType {
|
// Server returns the internal *http.Server.
|
||||||
case engine.FastHTTP:
|
func (e *Echo) Server(addr string) *http.Server {
|
||||||
e.engine = fasthttp.NewServer(config, handler)
|
s := &http.Server{Addr: addr, Handler: e}
|
||||||
|
// TODO: Remove in Go 1.6+
|
||||||
|
if e.http2 {
|
||||||
|
http2.ConfigureServer(s, nil)
|
||||||
|
}
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
e.engine.Start()
|
// Run runs a server.
|
||||||
|
func (e *Echo) Run(addr string) {
|
||||||
// e.run(e.Server(addr))
|
e.run(e.Server(addr))
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunTLS runs a server with TLS configuration.
|
// RunTLS runs a server with TLS configuration.
|
||||||
@ -645,13 +618,94 @@ func (e *HTTPError) Error() string {
|
|||||||
return e.message
|
return e.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (binder) Bind(r engine.Request, i interface{}) (err error) {
|
// wrapMiddleware wraps middleware.
|
||||||
ct := r.Header().Get(ContentType)
|
func wrapMiddleware(m Middleware) MiddlewareFunc {
|
||||||
|
switch m := m.(type) {
|
||||||
|
case MiddlewareFunc:
|
||||||
|
return m
|
||||||
|
case func(HandlerFunc) HandlerFunc:
|
||||||
|
return m
|
||||||
|
case HandlerFunc:
|
||||||
|
return wrapHandlerFuncMW(m)
|
||||||
|
case func(Context) error:
|
||||||
|
return wrapHandlerFuncMW(m)
|
||||||
|
case func(http.Handler) http.Handler:
|
||||||
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
|
return func(c Context) (err error) {
|
||||||
|
x := c.X()
|
||||||
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
x.response.writer = w
|
||||||
|
x.request = r
|
||||||
|
err = h(c)
|
||||||
|
})).ServeHTTP(x.response.writer, x.request)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case http.Handler:
|
||||||
|
return wrapHTTPHandlerFuncMW(m.ServeHTTP)
|
||||||
|
case func(http.ResponseWriter, *http.Request):
|
||||||
|
return wrapHTTPHandlerFuncMW(m)
|
||||||
|
default:
|
||||||
|
panic("unknown middleware")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wrapHandlerFuncMW wraps HandlerFunc middleware.
|
||||||
|
func wrapHandlerFuncMW(m HandlerFunc) MiddlewareFunc {
|
||||||
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
|
return func(c Context) error {
|
||||||
|
if err := m(c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return h(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wrapHTTPHandlerFuncMW wraps http.HandlerFunc middleware.
|
||||||
|
func wrapHTTPHandlerFuncMW(m http.HandlerFunc) MiddlewareFunc {
|
||||||
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
|
return func(c Context) error {
|
||||||
|
x := c.X()
|
||||||
|
if !x.response.committed {
|
||||||
|
m.ServeHTTP(x.response.writer, x.request)
|
||||||
|
}
|
||||||
|
return h(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wrapHandler wraps handler.
|
||||||
|
func wrapHandler(h Handler) HandlerFunc {
|
||||||
|
switch h := h.(type) {
|
||||||
|
case HandlerFunc:
|
||||||
|
return h
|
||||||
|
case func(Context) error:
|
||||||
|
return h
|
||||||
|
case http.Handler, http.HandlerFunc:
|
||||||
|
return func(c Context) error {
|
||||||
|
x := c.X()
|
||||||
|
h.(http.Handler).ServeHTTP(x.response, x.request)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case func(http.ResponseWriter, *http.Request):
|
||||||
|
return func(c Context) error {
|
||||||
|
x := c.X()
|
||||||
|
h(x.response, x.request)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("unknown handler")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (binder) Bind(r *http.Request, i interface{}) (err error) {
|
||||||
|
ct := r.Header.Get(ContentType)
|
||||||
err = UnsupportedMediaType
|
err = UnsupportedMediaType
|
||||||
if strings.HasPrefix(ct, ApplicationJSON) {
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
||||||
err = json.NewDecoder(r.Body()).Decode(i)
|
err = json.NewDecoder(r.Body).Decode(i)
|
||||||
} else if strings.HasPrefix(ct, ApplicationXML) {
|
} else if strings.HasPrefix(ct, ApplicationXML) {
|
||||||
err = xml.NewDecoder(r.Body()).Decode(i)
|
err = xml.NewDecoder(r.Body).Decode(i)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
28
group.go
28
group.go
@ -6,48 +6,52 @@ type (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (g *Group) Use(m ...MiddlewareFunc) {
|
func (g *Group) Use(m ...Middleware) {
|
||||||
for _, h := range m {
|
for _, h := range m {
|
||||||
g.echo.middleware = append(g.echo.middleware, h)
|
g.echo.middleware = append(g.echo.middleware, wrapMiddleware(h))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Connect(path string, h HandlerFunc) {
|
func (g *Group) Connect(path string, h Handler) {
|
||||||
g.echo.Connect(path, h)
|
g.echo.Connect(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Delete(path string, h HandlerFunc) {
|
func (g *Group) Delete(path string, h Handler) {
|
||||||
g.echo.Delete(path, h)
|
g.echo.Delete(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Get(path string, h HandlerFunc) {
|
func (g *Group) Get(path string, h Handler) {
|
||||||
g.echo.Get(path, h)
|
g.echo.Get(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Head(path string, h HandlerFunc) {
|
func (g *Group) Head(path string, h Handler) {
|
||||||
g.echo.Head(path, h)
|
g.echo.Head(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Options(path string, h HandlerFunc) {
|
func (g *Group) Options(path string, h Handler) {
|
||||||
g.echo.Options(path, h)
|
g.echo.Options(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Patch(path string, h HandlerFunc) {
|
func (g *Group) Patch(path string, h Handler) {
|
||||||
g.echo.Patch(path, h)
|
g.echo.Patch(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Post(path string, h HandlerFunc) {
|
func (g *Group) Post(path string, h Handler) {
|
||||||
g.echo.Post(path, h)
|
g.echo.Post(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Put(path string, h HandlerFunc) {
|
func (g *Group) Put(path string, h Handler) {
|
||||||
g.echo.Put(path, h)
|
g.echo.Put(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Trace(path string, h HandlerFunc) {
|
func (g *Group) Trace(path string, h Handler) {
|
||||||
g.echo.Trace(path, h)
|
g.echo.Trace(path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *Group) WebSocket(path string, h HandlerFunc) {
|
||||||
|
g.echo.WebSocket(path, h)
|
||||||
|
}
|
||||||
|
|
||||||
func (g *Group) Static(path, root string) {
|
func (g *Group) Static(path, root string) {
|
||||||
g.echo.Static(path, root)
|
g.echo.Static(path, root)
|
||||||
}
|
}
|
||||||
@ -60,6 +64,6 @@ func (g *Group) ServeFile(path, file string) {
|
|||||||
g.echo.ServeFile(path, file)
|
g.echo.ServeFile(path, file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Group) Group(prefix string, m ...MiddlewareFunc) *Group {
|
func (g *Group) Group(prefix string, m ...Middleware) *Group {
|
||||||
return g.echo.Group(prefix, m...)
|
return g.echo.Group(prefix, m...)
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,11 @@ const (
|
|||||||
func BasicAuth(fn BasicValidateFunc) echo.HandlerFunc {
|
func BasicAuth(fn BasicValidateFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
// Skip WebSocket
|
// Skip WebSocket
|
||||||
if (c.Request().Header().Get(echo.Upgrade)) == echo.WebSocket {
|
if (c.Request().Header.Get(echo.Upgrade)) == echo.WebSocket {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := c.Request().Header().Get(echo.Authorization)
|
auth := c.Request().Header.Get(echo.Authorization)
|
||||||
l := len(Basic)
|
l := len(Basic)
|
||||||
|
|
||||||
if len(auth) > l+1 && auth[:l] == Basic {
|
if len(auth) > l+1 && auth[:l] == Basic {
|
||||||
|
@ -53,7 +53,7 @@ func Gzip() echo.MiddlewareFunc {
|
|||||||
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
return func(h echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
||||||
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
if strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) {
|
||||||
w := writerPool.Get().(*gzip.Writer)
|
w := writerPool.Get().(*gzip.Writer)
|
||||||
w.Reset(c.Response().Writer())
|
w.Reset(c.Response().Writer())
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -16,9 +16,9 @@ func Logger() echo.MiddlewareFunc {
|
|||||||
logger := c.Logger()
|
logger := c.Logger()
|
||||||
|
|
||||||
remoteAddr := req.RemoteAddr
|
remoteAddr := req.RemoteAddr
|
||||||
if ip := req.Header().Get(echo.XRealIP); ip != "" {
|
if ip := req.Header.Get(echo.XRealIP); ip != "" {
|
||||||
remoteAddr = ip
|
remoteAddr = ip
|
||||||
} else if ip = req.Header().Get(echo.XForwardedFor); ip != "" {
|
} else if ip = req.Header.Get(echo.XForwardedFor); ip != "" {
|
||||||
remoteAddr = ip
|
remoteAddr = ip
|
||||||
} else {
|
} else {
|
||||||
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
|
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
|
||||||
|
18
router.go
18
router.go
@ -275,8 +275,7 @@ func (n *node) check405() HandlerFunc {
|
|||||||
return notFoundHandler
|
return notFoundHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Find(method, path string, context Context) (h HandlerFunc, e *Echo) {
|
func (r *Router) Find(method, path string, x *context) (h HandlerFunc, e *Echo) {
|
||||||
x := context.X()
|
|
||||||
h = notFoundHandler
|
h = notFoundHandler
|
||||||
e = r.echo
|
e = r.echo
|
||||||
cn := r.tree // Current node as root
|
cn := r.tree // Current node as root
|
||||||
@ -402,12 +401,11 @@ End:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
// TODO: v2
|
c := r.echo.pool.Get().(*context)
|
||||||
// c := r.echo.pool.Get().(*context)
|
h, _ := r.Find(req.Method, req.URL.Path, c)
|
||||||
// h, _ := r.Find(req.Method, req.URL.Path, c)
|
c.reset(req, w, r.echo)
|
||||||
// c.reset(req, w, r.echo)
|
if err := h(c); err != nil {
|
||||||
// if err := h(c); err != nil {
|
r.echo.httpErrorHandler(err, c)
|
||||||
// r.echo.httpErrorHandler(err, c)
|
}
|
||||||
// }
|
r.echo.pool.Put(c)
|
||||||
// r.echo.pool.Put(c)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user