Gin: How remove route ? online!!!

Created on 29 Dec 2016  Â·  24Comments  Â·  Source: gin-gonic/gin

How can i remove route ? online!!!

feature

Most helpful comment

You really should try to write a more verbose description for this issue before hand.
Nevertheless I think that a simple middleware might be what you're looking for. Then just wrap the routes with the middleware and check if active/inactive before going to the actual route.

All 24 comments

Why ? What is your scenario?

i need to remove the routes online and add the routing rules online

@monikaYZ did you mean altering your application routes on-the-fly?

and I did not find the corresponding method。

@widnyana Yes!

@monikaYZ I'm afraid you can't do that. since your app source code are compiled to binary. you need to alter your source code, recompile, and deploy it again.

pardon me if I miss-interpret your question.

I need to do a configuration service, and configure memory

if your service runs behind nginx, maybe you can alter nginx config to set api online/offline

You really should try to write a more verbose description for this issue before hand.
Nevertheless I think that a simple middleware might be what you're looking for. Then just wrap the routes with the middleware and check if active/inactive before going to the actual route.

@monikaYZ I understand what you mean.

He means to inject/remove dynamic handlers into Gin router. Although is possible to inject them after starting the server, there is no API for removing them. There may be some concurrency problems if you change the gin.Engine from different goroutines.

I'm also facing that problem, and developing a custom solution which I'll post to https://github.com/gin-contrib/graceful when ready.

I'll keep this issue open until then.

Hi there

I have multiple devices which each one after registration get a token(hash string) and uses that in order to communicate with server. So I need to add/remove routes when a token add/remove from system...

Again, is it possible to add/remove routes at runtime?

Thanks in advance

Hi @bonjefir,
More devices doesn’t mean you need one route for each of them. You can send token as parameter in post data or request header. Hope this helps.

Similar use case here, I would like to attach routing subtrees in order to make entire sections available or unavailable dynamically. Restarting the http handler is not a problem.

same problem. I don't know which url should be route before i load config from cloud; and if cloud config changes, i need remove/add router on the fly. Now, i can just enable all router but return 404 when config changed.

same problem. it is perfect that gin supports altering handler in the routeTree.for dynamicly reloading handler.

I need the feature, also.

Nginx CAN do this more conveniently. If your app are running on production, add/remove routes in your code at runtime is danger.

Unless you need to load application modules and join them to a single API. But I found that creating generic endpoints and then manually using a forked version of httprouter to match the loaded ones works best, so this is not a problem for me anymore.

I need the feature, also.

Here is the way to implement change routes dynamically by change the whole handler.

https://gist.github.com/wudi/eb778531ff83ee1273f58aa7ddb353fe

I added the following in gin.go:

// Remove ...
func (engine *Engine) Remove(path string, tbRemoved HandlerFunc) {
    for _, tree := range engine.trees {
        for i, children := range tree.root.children {
            for _, h := range children.handlers {
                if reflect.ValueOf(h) == reflect.ValueOf(tbRemoved) && children.path == path {
                    tree.root.children = remove(tree.root.children, i)
                }
            }
        }
    }
}

func remove(s []*node, index int) []*node {
    return append(s[:index], s[index+1:]...)
}

Is totally unsafe and causes panicrecovery but works ...

Also need this.
My scenario is use gin as api gateway, so need change routes

I'm currently using this solution described in fiber

Edit: I've been searching a lot in web routers frameworks, but the most similar as I need has some problems with this feature because it's unsafe... You could use fiber solution if it's enough, but if you really need add/remove routes on-the-fly I strongly recommend Iris

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cachafla picture cachafla  Â·  33Comments

ndbroadbent picture ndbroadbent  Â·  67Comments

selvam347 picture selvam347  Â·  19Comments

libnat picture libnat  Â·  29Comments

miketonks picture miketonks  Â·  25Comments