1

I have a service named alpha (created using python-django) that runs on http://127.0.0.1:9000 and has these two endpoings

  • /health returns {"health": "OK"} status 200
  • /codes/<str:code> returns {"code": code} status 200

I also have a kong api-gateway in the db-less declarative mode that runs on localhost port 80

in kong.yaml I have two services

services:
  - name: local-alpha-health
    url: http://host.docker.internal:9000/health
    routes:
      - name: local-alpha-health
        methods:
          - GET
        paths:
          - /alpha/health
        strip_path: true
  - name: local-alpha-code
    url: http://host.docker.internal:9000/code/ # HOW TO WRITE THIS PART???
    routes:
      - name: local-alpha-code
        methods:
          - GET
        paths:
          - /alpha/code/(?<appcode>\d+) # Is this right???
        strip_path: true
  • If I send a GET request to http://127.0.0.1/alpha/health it returns {"health": "OK"} status 200 which shows kong is working.
  • I want to send a request such as http://127.0.0.1/alpha/code/123 and I expect to receive {"code": 123} status 200 but I don't know how to setup kong.yaml file to do this. If I send a request to http://127.0.0.1/alpha/code/123 I get 404 from (from the alpha django application) which means kong is routing the request to alpha service but if I send a request to http://127.0.0.1/alpha/code/abc I get {"message": "no Route matched with those values"} which shows the regex is working

I could do this

services:
  - name: local-alpha-health
    url: http://host.docker.internal:9000/
    routes:
      - name: local-alpha-health
        methods:
          - GET
        paths:
          - /alpha
        strip_path: true

Then a request sent to http://127.0.0.1/alpha/code/123 would go to ``http://127.0.0.1:9000/code/123` but I cannot control with regex

Any idea How to route requests to a dynamic endpoint on kong api-gateway?

This content seems related but cannot figure it out how to set it up https://docs.konghq.com/gateway-oss/2.5.x/proxy/

Amin Ba
  • 1,603
  • 1
  • 13
  • 38

1 Answers1

1

Note that a request like http://127.0.0.1/alpha/code/abc will indeed not match the rule you have added, because of the \d+ (which matches one or more digits). Also, http://127.0.0.1/alpha/code/123 will reach the upstream as a request to /, since you have strip_path set to true.

I have tested your example with some minor tweaks to proxy to a local httpbin service, which has a similar endpoint (/status/<code>).

Start a local httpbin service:

$ docker run --rm -d -p "8080:80" kennethreitz/httpbin

Start Kong with the following config:

_format_version: "2.1"

services:
  - name: local-alpha-code
    url: http://localhost:8080
    routes:
      - name: local-mockbin-status
        methods:
          - GET
        paths:
          - /status/(?<appcode>\d+)
        strip_path: false

Note that strip_path is set to false, so the entire matching path is proxied to the upstream.

Test it out with:

$ http :8000/status/200                                       
HTTP/1.1 200 OK
  • 1
    are you sure about this: `Also, http://127.0.0.1/alpha/code/123 will reach the upstream as a request to /, since you have strip_path set to true.` I think it will reach upstream as request to `/code` because my url is `http://host.docker.internal:9000/code/` – Amin Ba Jun 15 '22 at 18:45