-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
How to remove a routing? #1507
Comments
Iris had expose the feature to remove a route at runtime but it's not safe, thats why gin does not have it too. The best option is made by Iris, which you can change the status of a route (online, offline), currently an offline route is still reachable through a custom 'NONE' http method but you can change that behavior by adding a middleware that checks the route's status and fire 404 not found to the client. EDIT: If you can describe me why you want such an action I would possible think a complete feature that could be implemented into Iris to help you do it correctly and safety. |
Another way is described below, however it's not recommended and it's unsafe to use in production systems. You may think to shutdown the entire server, rebuild the application and re-run. However, the best solution is described in my previous comment: just add a middleware and fire package main
import (
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
app.Get("/replace_route", func(ctx iris.Context) {
newMethodRoutes := app.CreateRoutes([]string{iris.MethodGet}, "/route", newHandler)
app.AddRouteUnsafe(newMethodRoutes[0])
ctx.Writef("go to /route now and see the newHandler be the only handler to be executed")
})
app.Get("/remove_route", func(ctx iris.Context) {
// for clients this route is removed, not accessible because of the "notFound" handler registered,
// if the server wants to replace it, it can call AddRouteUnsafe again,
// if necessary use mutexes on handlers.
newMethodRoutes := app.CreateRoutes([]string{iris.MethodGet}, "/route", notFound)
app.AddRouteUnsafe(newMethodRoutes[0])
})
app.Get("/route", func(ctx iris.Context) {
ctx.Writef("it's the 'route', go to /replace_route to change it")
})
app.Listen(":8080")
}
func newHandler(ctx iris.Context) {
ctx.Writef("new handler for the 'route'")
}
func notFound(ctx iris.Context) {
ctx.NotFound()
} Notes: The |
oh,I've tried to override the route to NotFound,But I have a large number of routes. When I register 10000 routes, using app.refreshrouter again will increase the refresh time, so I want to see if there is any way to completely delete routes. At present, I'll use rewriting route to 404 for production,Thank you. |
Why you need to remove so many routes in production? A wildcard route would be more practical for dynamic routing |
I just take, for example, just as much as possible to reduce the route,The wildcard can't meet my needs,Are user-defined routing,Unless my own matching routing rules |
I watched all of the sample is only hidden routing
Whether can delete a route after the visit is 404
I am in iris issuse didn't find about delete the routing problem,But I found out the related problems in gin.
gin-gonic/gin#776
The text was updated successfully, but these errors were encountered: