Note: I am not sure if the question belongs to this particular stackexchange . If it does not, please let me know and I will delete it here and post it on the relevant exchange. Though it seemed more suited to this exchange than StackOverflow.
I am trying to create a REST server. My problem is to optimise a router/route multiplexer(mux for short)
The use case is as follows :
At the start , I register multiple routes , example - /route/v1/{someParam}/end
and /route/v2/{someParam}/notEnd/somethingElse
, For each route we have a corresponding callback function that we can call . In the case of any new request, we get a new URI and find out which route it matches and call the corresponding callback function. So the code will look like
auto Callback = match(URIFromRequest)
Callback() // Called in a separate thread . Not our concern now
Please keep in mind that {someParam}
in the route is a path parameter so the URIFromRequest /route/v1/randomValue/end
matches /route/v1/{someParam}/end
.
My goal is to create a Data Structure of the routes and their corresponding callback such that the match function is as fast as possible. Please Note that the total number of routes registered will be only around 30-50.
Right now I am planning to convert each registered route to regex. For example ,the /route/v1/{someParam}/end
gets converted to regex like ^/route/v1/[^/]+/end$
.
Now, all these regexes and corresponding callbacks are stored in a map-like structure. Whenever I get a new request, I iterate through the map. If the URI, matches any request, I return its callback function.
This seems terribly inefficient, however, I cannot think of any other solution that also supports path-parameters(i.e the {someParam} part). Does anyone have any idea on what should be a more efficient solution?
/{controller}/{id}
is so common. It hits so many URL patterns for REST services that you don't need many other specs.