mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
parent
8aae962c95
commit
1f8ecfd610
37
echo.go
37
echo.go
@ -34,8 +34,8 @@ type (
|
||||
renderer Renderer
|
||||
pool sync.Pool
|
||||
debug bool
|
||||
stripTrailingSlash bool
|
||||
router *Router
|
||||
// stripTrailingSlash bool
|
||||
router *Router
|
||||
}
|
||||
|
||||
Route struct {
|
||||
@ -262,9 +262,9 @@ func (e *Echo) Debug() bool {
|
||||
}
|
||||
|
||||
// StripTrailingSlash enables removing trailing slash from the request path.
|
||||
func (e *Echo) StripTrailingSlash() {
|
||||
e.stripTrailingSlash = true
|
||||
}
|
||||
// func (e *Echo) StripTrailingSlash() {
|
||||
// e.stripTrailingSlash = true
|
||||
// }
|
||||
|
||||
// Use adds handler to the middleware chain.
|
||||
func (e *Echo) Use(m ...Middleware) {
|
||||
@ -463,27 +463,12 @@ func (e *Echo) Routes() []Route {
|
||||
|
||||
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
|
||||
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
e.router.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// Server returns the internal *http.Server.
|
||||
func (e *Echo) Server(addr string) *http.Server {
|
||||
s := &http.Server{Addr: addr}
|
||||
s.Handler = e
|
||||
s := &http.Server{Addr: addr, Handler: e}
|
||||
if e.http2 {
|
||||
http2.ConfigureServer(s, nil)
|
||||
}
|
||||
@ -497,9 +482,9 @@ func (e *Echo) Run(addr string) {
|
||||
}
|
||||
|
||||
// RunTLS runs a server with TLS configuration.
|
||||
func (e *Echo) RunTLS(addr, certFile, keyFile string) {
|
||||
func (e *Echo) RunTLS(addr, crtFile, keyFile string) {
|
||||
s := e.Server(addr)
|
||||
e.run(s, certFile, keyFile)
|
||||
e.run(s, crtFile, keyFile)
|
||||
}
|
||||
|
||||
// RunServer runs a custom server.
|
||||
@ -508,8 +493,8 @@ func (e *Echo) RunServer(s *http.Server) {
|
||||
}
|
||||
|
||||
// RunTLSServer runs a custom server with TLS configuration.
|
||||
func (e *Echo) RunTLSServer(s *http.Server, certFile, keyFile string) {
|
||||
e.run(s, certFile, keyFile)
|
||||
func (e *Echo) RunTLSServer(s *http.Server, crtFile, keyFile string) {
|
||||
e.run(s, crtFile, keyFile)
|
||||
}
|
||||
|
||||
func (e *Echo) run(s *http.Server, files ...string) {
|
||||
|
16
echo_test.go
16
echo_test.go
@ -406,14 +406,14 @@ func TestEchoServer(t *testing.T) {
|
||||
assert.IsType(t, &http.Server{}, s)
|
||||
}
|
||||
|
||||
func TestStripTrailingSlash(t *testing.T) {
|
||||
e := New()
|
||||
e.StripTrailingSlash()
|
||||
r, _ := http.NewRequest(GET, "/users/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
e.ServeHTTP(w, r)
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
// func TestStripTrailingSlash(t *testing.T) {
|
||||
// e := New()
|
||||
// e.StripTrailingSlash()
|
||||
// r, _ := http.NewRequest(GET, "/users/", nil)
|
||||
// w := httptest.NewRecorder()
|
||||
// e.ServeHTTP(w, r)
|
||||
// assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
// }
|
||||
|
||||
func testMethod(t *testing.T, method, path string, e *Echo) {
|
||||
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
|
||||
|
30
router.go
30
router.go
@ -272,12 +272,12 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
|
||||
cn := r.tree // Current node as root
|
||||
|
||||
// Strip trailing slash
|
||||
if r.echo.stripTrailingSlash {
|
||||
l := len(path) - 1
|
||||
if path != "/" && path[l] == '/' { // Issue #218
|
||||
path = path[:l]
|
||||
}
|
||||
}
|
||||
// if r.echo.stripTrailingSlash {
|
||||
// l := len(path) - 1
|
||||
// if path != "/" && path[l] == '/' { // Issue #218
|
||||
// path = path[:l]
|
||||
// }
|
||||
// }
|
||||
|
||||
var (
|
||||
search = path
|
||||
@ -398,10 +398,18 @@ End:
|
||||
|
||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
c := r.echo.pool.Get().(*Context)
|
||||
h, _ := r.Find(req.Method, req.URL.Path, c)
|
||||
c.reset(req, w, r.echo)
|
||||
if err := h(c); err != nil {
|
||||
r.echo.httpErrorHandler(err, c)
|
||||
h, e := r.Find(req.Method, req.URL.Path, c)
|
||||
c.reset(req, w, e)
|
||||
|
||||
// Chain middleware with handler in the end
|
||||
for i := len(e.middleware) - 1; i >= 0; i-- {
|
||||
h = e.middleware[i](h)
|
||||
}
|
||||
r.echo.pool.Put(c)
|
||||
|
||||
// Execute chain
|
||||
if err := h(c); err != nil {
|
||||
e.httpErrorHandler(err, c)
|
||||
}
|
||||
|
||||
e.pool.Put(c)
|
||||
}
|
||||
|
@ -28,9 +28,3 @@ Enables/disables debug mode.
|
||||
### Disable colored log
|
||||
|
||||
`Echo.DisableColoredLog()`
|
||||
|
||||
### StripTrailingSlash
|
||||
|
||||
StripTrailingSlash enables removing trailing slash from the request path.
|
||||
|
||||
`e.StripTrailingSlash()`
|
||||
|
Loading…
Reference in New Issue
Block a user