-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can add NoRoute(h HandlerFunc, m ...MiddlewareFunc) to Group and Echo? #856
Comments
What is the function of |
NoRoute() have more advantage than ErrorHandler!
NoRoute() example as follow: 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: 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. |
How come this was never added? I'm confused on how others use Angular with Echo when there are several resources file Angular ask at initialization. I tried doing this hack:
but then that capture all requests to any other paths (e.g something.com/this-path/) even if there is a e.group assigned. |
If you want to use single page applications. Route all non "rest" requests to static assets - then Static middleware is something that you should use. func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "./website",
Index: "index.html",
// Enable HTML5 mode by forwarding all not-found requests to root so that
// SPA (single-page application) can handle the routing.
HTML5: true,
}))
api := e.Group("/api")
api.GET("/pairs", func(c echo.Context) error {
return c.JSON(http.StatusOK, map[string]string{"key": "value"})
})
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} |
Note: there are plenty of different ways to replace 404. ErrorHandler is just one place You can create Any route doing the same func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Any("/*", func(c echo.Context) error {
return c.JSON(http.StatusNotFound, "NOT FOUND MESSAGE")
})
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} You can even replace default 404 function as it is public func main() {
// or replace default NotFoundHandler
echo.NotFoundHandler = func(c echo.Context) error {
return c.JSON(http.StatusNotFound, "custom message")
}
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} |
cfg := engine.Group("/config", authConfig) // authConfig is middleware handler
........
cfg.NoRoute(func(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, xxxx)
})
xxx := engine.Group("/xxxx", authConfig) // authConfig is middleware handler
........
xxxx.NoRoute(func(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, xxxx)
}) |
@aldas Thank you for that solution |
Description
Can add NoRoute(h HandlerFunc, m ...MiddlewareFunc) to Group and Echo?
There are NoRoute() in https://github.com/gin-gonic/gin.
NoRoute() usage:
The text was updated successfully, but these errors were encountered: