I have a route matching question

Hello, it is my first time using Kong gateway.

I have a route matching question.

  • Kong version: 2.1.3

I have some REST URI like these:

  1. POST /api/v1/user
  2. GET /api/v1/user/{:userid}/verify?code={code}
  3. GET /api/v1/user/{:userid}/exist
  4. POST /api/v1/user/{:userid}/password/reset
  5. GET /api/v1/user/{:userid}

I want to 1,2,3,4 cases route to upstream A, and 5 cases route to upstream B.

What should I do with the rule of the path?

I have read this document: Proxy Reference - v2.1.x | Kong - Open-Source API Management and Microservice Management, and have no idea to write the rules and regex_priority.

Hello,

Two routes should be able to do the trick here; one way to do it could be:

  1. First route with paths[]=/api/v1/user(/\d+/.*)? should capture 1, 2, 3, 4.
  2. Second route with methods[]=GET paths[]=/api/v1/user/\d+

Since route 2 has more matching attributes than 1 it should be evaluated first, and you may add regex_priority=10 to the second route for future proofing (the default regex_priority is 0, and a higher priority means the route’s regex will be evaluated first).

Of course, you must make sure that these routes do not conflict with any other routes you may be configuring for other services. Adjust the regexes and matching rules accordingly to your needs and help yourself with a regex matching tool such as https://regex101.com/.

Additionally, if you wish to capture the {userid} segment at evaluation time (instead of writing extra code to retrieve it yourself from the URL), you may specify your regexes with a capturing group, for example:

paths[]=/api/v1/user(/(?<userid>\d+)/.*)?

Then retrieve the user id in your plugin(s):

-- must use ngx.ctx and not kong.ctx here until PDK implements URI captured groups lookup
local userid = ngx.ctx.router_matches.uri_captures.userid

To get the code querystring parameter, use the PDK (Plugin Development Kit) as such:

local code = kong.request.get_query_arg("code")

Hope this helps.


© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ