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

Use another router (such as gorilla mux) with Gin #1162

Closed
lansana opened this issue Nov 9, 2017 · 2 comments
Closed

Use another router (such as gorilla mux) with Gin #1162

lansana opened this issue Nov 9, 2017 · 2 comments

Comments

@lansana
Copy link

lansana commented Nov 9, 2017

Is it possible to plug in another router, such as gorilla mux, with Gin?

I am trying to add certain REST endpoints, but this router does not allow them. I'm at the point now where I've (unfortunately) coupled the router and it's grouping mechanisms tightly to my app, so migrating to a new router will be a tedious process.

I really like the way gin with it's versioning and middleware and so on; it is perfect for my use cases. It just has one problem which is the lack of ability to use standard REST endpoints. Having to do really ugly things with query params to solve this problem.

The problem is, I sometimes need endpoints like /items/username/:username, /items/:id/foo, /items/:slug/bar and so on all at the same time, but this router does not allow mixing those endpoints together because of the ambiguity. It can't differentiate the three obviously different paths.

How could I swap out the authorized.GET with a mux router.Get? Anyone have a solution to this?:

        authorized := c.Router.Group("/v1").Use(middleware.Authorized())
	{
                authorized.GET(
			"/items",
			middleware.RateLimit(100, time.Minute * 5),
			ctrl.AllPaginated,
		)
		authorized.POST(
			"/items",
			middleware.RateLimit(100, time.Minute * 5),
			ctrl.Create,
		)
		authorized.PUT(
			"/items/:slug",
			middleware.RateLimit(100, time.Minute * 5),
			ctrl.UpdateBySlug,
		)
		authorized.DELETE(
			"/items/:slug",
			middleware.RateLimit(100, time.Minute * 5),
			ctrl.SoftDeleteBySlug,
		)
	}
@lansana lansana changed the title Use Gorilla mux with Gin Use another router (such as gorilla mux) with Gin Nov 9, 2017
@robvdl
Copy link

robvdl commented Nov 9, 2017

I don't see the point of swapping out Gin's router, then there isn't much of Gin left. You might as well use Chi, which has some nice ways to mount nested routers and then you don't need Gorilla Mux at all. Or if you really must use Gorilla Mux, then Negroni might be a better fit as that framework is all about bring your own router.

@lansana
Copy link
Author

lansana commented Nov 9, 2017

@robvdl Thanks for the suggestion, I actually just started refactoring to use Chi a couple hours ago. Works exactly how I need with the proper REST endpoints, and supports same style of middleware/grouping as Gin.

The reason I wanted to swap out is I wanted to keep the Gin and middleware/grouping, while using another routers path matcher with the GET, POST, etc. methods.

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

No branches or pull requests

2 participants