Can add NoRoute(h HandlerFunc, m ...MiddlewareFunc) to Group and Echo?
There are NoRoute() in https://github.com/gin-gonic/gin.
NoRoute() usage:
package mai
func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
engine.NoRoute(http.DefaultServeMux)
q := engine.Group("/query")
q.GET("/hello", func(ctx echo.Context) error {
ctx.String(http.StatusOK "hi!")
})
q.NoRoute(func(ctx echo.Context) error {
ctx.String(http.StatusNotFound, "not found with prefix is /query")
})
}
What is the function of NoRoute? If you are looking for handling route not found requests look at https://echo.labstack.com/guide/error-handling. Echo#DefaultErrorHandler captures all the errors and it can also be customized.
NoRoute() have more advantage than ErrorHandler!
NoRoute() example as follow:
````go
package mai
func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
engine.NoRoute(http.DefaultServeMux)
q := engine.Group("/query", authQuery) // authQueryis middleware handler,
q.GET("/hello", func(ctx echo.Context) error {
return ctx.String(http.StatusOK "hi!")
})
q.NoRoute(func(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, xxxx)
}, auth) // auth is middleware
cfg := engine.Group("/config", authConfig) // authConfig is middleware handler,
cfg.GET("/hello", func(ctx echo.Context) error {
return ctx.String(http.StatusOK "hi!")
})
cfg.NoRoute(func(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, xxxx)
})
}
````
ErrorHandler() example as follow:
````go
package mai
func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
q := engine.Group("/query", auth) // auth is middleware handler,
q.GET("/hello", func(ctx echo.Context) error {
return ctx.String(http.StatusOK "hi!")
})
cfg := engine.Group("/config", auth) // auth is middleware handler,
cfg.GET("/hello", func(ctx echo.Context) error {
return ctx.String(http.StatusOK "hi!")
})
engine.ErrorHandler = func(e error, ctx Context) error {
if e == echo.ErrNotFound {
if strings.Contains(ctx.Request().URL.Path, "/query") {
// hack! it isn't standard for run handler
authQuery(ctx, func(ctx Context) {
return ctx.JSON(http.StatusOK, queryxxxx)
} )
return nil
} else if strings.Contains(ctx.Request().URL.Path, "/config") {
// hack! it isn't standard for run handler
authConfig(ctx, func(ctx Context) {
return ctx.JSON(http.StatusOK, configxxxx)
} )
return nil
}
http.DefaultServeMux.ServeHttp(ctx.Response(), ctx.Request())
return nil
}
}
}
````
If you looking for example, you could see what gin does here http://godoc.org/github.com/gin-gonic/gin#Engine.NoRoute
It will return default 404 for un-registered routes 🙄
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
NoRoute() have more advantage than ErrorHandler!
NoRoute() example as follow:
````go
package mai
func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
engine.NoRoute(http.DefaultServeMux)
}
````
ErrorHandler() example as follow:
````go
package mai
func main() {
engine := echo.New()
engine.Use(middleware.Logger())
engine.Use(middleware.Recover())
}
````