Skip to content
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

Closed
basicfu opened this issue May 7, 2020 · 5 comments
Closed

How to remove a routing? #1507

basicfu opened this issue May 7, 2020 · 5 comments

Comments

@basicfu
Copy link

basicfu commented May 7, 2020

I watched all of the sample is only hidden routing

none.Method = iris.MethodNone
app.RefreshRouter()

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

@kataras
Copy link
Owner

kataras commented May 7, 2020

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.

@kataras
Copy link
Owner

kataras commented May 7, 2020

@basicfu

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 Not Found 404 if you want to disable a route, you can also get the current route's information from inside a handler with the ctx.GetCurrentRoute() method. I think you can't do that with gin.

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 AddRouteUnsafe was designed for the Sitemap feature, it is mostly useful when called right after the app.Listen/Run or Build, e.g. you want to register a route which should obtain or manipulate the built routes. The Sitemap feature does exactly that, it collects all registered and built routes and builds and exposes a sitemap route.

@basicfu
Copy link
Author

basicfu commented May 8, 2020

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.

@basicfu basicfu closed this as completed May 8, 2020
@kataras
Copy link
Owner

kataras commented May 8, 2020

Why you need to remove so many routes in production? A wildcard route would be more practical for dynamic routing

@basicfu
Copy link
Author

basicfu commented May 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants