Skip to content

Use dynamic route with URL parameters #94

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

Merged
merged 2 commits into from
Apr 24, 2020
Merged

Use dynamic route with URL parameters #94

merged 2 commits into from
Apr 24, 2020

Conversation

jorgebay
Copy link
Contributor

Resolves #92 .

With this patch, a route with the pattern /graphql/:keyspace is always created and the keyspace is obtained from the url path.

Copy link
Contributor

@mpenick mpenick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about providing a small interface to leverage routers libraries?

type RouteHandler interface {
	KeyspaceRoute(pattern string) string
	Keyspace(r *http.Request) string
}

type httpRouterHandler struct { }

func NewHttpRouterHandler() httpRouterHandler {
	return httpRouterHandler{}
}

func (h httpRouterHandler) KeyspaceRoute(pattern string) string {
	return path.Join(pattern, ":keyspace")
}

func (h httpRouterHandler) Keyspace(r *http.Request) string {
	params := httprouter.ParamsFromContext(r.Context())
	return params.ByName("keyspace")
}

Full patch: 1eaa7ca (updated)

@jorgebay
Copy link
Contributor Author

What do you think about providing a small interface to leverage routers libraries?

My only concern is that it requires more code from endpoint route user pov. For cloud, if they decide to switch to mux it will involve implementing this interface vs changing a setting.

@mpenick
Copy link
Contributor

mpenick commented Apr 23, 2020

That would also work with gorilla/mux:

type gorillaMuxHandler struct { }

func NewHttpRouterHandler() gorillaMuxHandler {
	return gorillaMuxHandler{}
}

func (h gorillaMuxHandler) KeyspaceRoute(pattern string) string {
	return path.Join(pattern, "{keyspace}")
}

func (h gorillaMuxHandler) Keyspace(r *http.Request) string {
  vars := mux.Vars(r)
  return vars["keyspace"]
}

@mpenick
Copy link
Contributor

mpenick commented Apr 23, 2020

What do you think about providing a small interface to leverage routers libraries?

My only concern is that it requires more code from endpoint route user pov. For cloud, if they decide to switch to mux it will involve implementing this interface vs changing a setting.

It ends up being a very small amount of stateless code and using a regex doesn't leverage the router's capabilities so we end up double parsing the url.

@jorgebay
Copy link
Contributor Author

I think exposing less API surface for cloud integration and simple settings are a plus, even at the cost of being less extensible and parsing the url ourselves, but I don't have strong feelings about it.

I'll rebase this and address the lock optimization.

@jorgebay
Copy link
Contributor Author

Done! Feel free to push the RouteHandler interface directly to this branch.

@mpenick
Copy link
Contributor

mpenick commented Apr 24, 2020

Secret option C: Because we're not leveraging the URL templating in either router, meaning we don't need to have the syntax :keyspace or {keyspace}. What about having no option at all?

We know that keyspace is the last segment in the pattern: /some/pattern/<keyspace>. Why not just split that off using something like path.Base()?

@jorgebay
Copy link
Contributor Author

Secret option C

Interesting... I'm not sure I understand how routers would match the path without a router-based expression like {param} or :param? afaik, there's no built way to do that in http package,.

@jorgebay jorgebay merged commit e947440 into master Apr 24, 2020
@jorgebay jorgebay deleted the dynamic-routes branch April 24, 2020 13:28
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

Successfully merging this pull request may close these issues.

Dynamically update keyspaces in the built-in server
2 participants