1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-13 01:30:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2015-11-15 11:30:53 -08:00
parent 8aae962c95
commit 1f8ecfd610
4 changed files with 38 additions and 51 deletions

37
echo.go
View File

@ -34,8 +34,8 @@ type (
renderer Renderer renderer Renderer
pool sync.Pool pool sync.Pool
debug bool debug bool
stripTrailingSlash bool // stripTrailingSlash bool
router *Router router *Router
} }
Route struct { Route struct {
@ -262,9 +262,9 @@ func (e *Echo) Debug() bool {
} }
// StripTrailingSlash enables removing trailing slash from the request path. // StripTrailingSlash enables removing trailing slash from the request path.
func (e *Echo) StripTrailingSlash() { // func (e *Echo) StripTrailingSlash() {
e.stripTrailingSlash = true // e.stripTrailingSlash = true
} // }
// Use adds handler to the middleware chain. // Use adds handler to the middleware chain.
func (e *Echo) Use(m ...Middleware) { func (e *Echo) Use(m ...Middleware) {
@ -463,27 +463,12 @@ 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) {
c := e.pool.Get().(*Context) e.router.ServeHTTP(w, r)
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. // Server returns the internal *http.Server.
func (e *Echo) Server(addr string) *http.Server { func (e *Echo) Server(addr string) *http.Server {
s := &http.Server{Addr: addr} s := &http.Server{Addr: addr, Handler: e}
s.Handler = e
if e.http2 { if e.http2 {
http2.ConfigureServer(s, nil) http2.ConfigureServer(s, nil)
} }
@ -497,9 +482,9 @@ func (e *Echo) Run(addr string) {
} }
// RunTLS runs a server with TLS configuration. // 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) s := e.Server(addr)
e.run(s, certFile, keyFile) e.run(s, crtFile, keyFile)
} }
// RunServer runs a custom server. // RunServer runs a custom server.
@ -508,8 +493,8 @@ func (e *Echo) RunServer(s *http.Server) {
} }
// RunTLSServer runs a custom server with TLS configuration. // RunTLSServer runs a custom server with TLS configuration.
func (e *Echo) RunTLSServer(s *http.Server, certFile, keyFile string) { func (e *Echo) RunTLSServer(s *http.Server, crtFile, keyFile string) {
e.run(s, certFile, keyFile) e.run(s, crtFile, keyFile)
} }
func (e *Echo) run(s *http.Server, files ...string) { func (e *Echo) run(s *http.Server, files ...string) {

View File

@ -406,14 +406,14 @@ func TestEchoServer(t *testing.T) {
assert.IsType(t, &http.Server{}, s) assert.IsType(t, &http.Server{}, s)
} }
func TestStripTrailingSlash(t *testing.T) { // func TestStripTrailingSlash(t *testing.T) {
e := New() // e := New()
e.StripTrailingSlash() // e.StripTrailingSlash()
r, _ := http.NewRequest(GET, "/users/", nil) // r, _ := http.NewRequest(GET, "/users/", nil)
w := httptest.NewRecorder() // w := httptest.NewRecorder()
e.ServeHTTP(w, r) // e.ServeHTTP(w, r)
assert.Equal(t, http.StatusNotFound, w.Code) // assert.Equal(t, http.StatusNotFound, w.Code)
} // }
func testMethod(t *testing.T, method, path string, e *Echo) { func testMethod(t *testing.T, method, path string, e *Echo) {
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:])) m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))

View File

@ -272,12 +272,12 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
cn := r.tree // Current node as root cn := r.tree // Current node as root
// Strip trailing slash // Strip trailing slash
if r.echo.stripTrailingSlash { // if r.echo.stripTrailingSlash {
l := len(path) - 1 // l := len(path) - 1
if path != "/" && path[l] == '/' { // Issue #218 // if path != "/" && path[l] == '/' { // Issue #218
path = path[:l] // path = path[:l]
} // }
} // }
var ( var (
search = path search = path
@ -398,10 +398,18 @@ End:
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
c := r.echo.pool.Get().(*Context) c := r.echo.pool.Get().(*Context)
h, _ := r.Find(req.Method, req.URL.Path, c) h, e := r.Find(req.Method, req.URL.Path, c)
c.reset(req, w, r.echo) c.reset(req, w, e)
if err := h(c); err != nil {
r.echo.httpErrorHandler(err, c) // 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)
} }

View File

@ -28,9 +28,3 @@ Enables/disables debug mode.
### Disable colored log ### Disable colored log
`Echo.DisableColoredLog()` `Echo.DisableColoredLog()`
### StripTrailingSlash
StripTrailingSlash enables removing trailing slash from the request path.
`e.StripTrailingSlash()`