Add service and route

To make our rover-api service accessible again, we will have to tell Kong two things:

  • that the API exists and how kong can call it

  • to add a route as an endpoint that is accessible from outside the (docker)network

All this is done by adding some lines to the configuration file we just created.

Add a service

From the perspective of Kong, which is inside the docker network, the API is available on the host rover-api. On this host the service is listening on port 8080. So we add the following to our configuration:

services:
  - name: rover-api
    host: rover-api
    port: 8080
    path: /rovers

Add a route

Now that Kong knows where to find our API we can add a route Kong will listen on. The following lines will add a route that forwards all requests made to http://<Kong-Server>/rovers that have one of the verbs ("GET","POST","PUT","DELETE") to our API.

We don’t need to reference the service, as we can define the route nested within the service.

    routes:
      - name: rover-api-route
        paths:
          - /rovers
        methods:
          - GET
          - POST
          - PUT
          - DELETE

Call the API

Start the services again with:

docker-compose up -d

Now the API can be called again with the same (cURL-)commands we used when we only had the API deployed. But now all communication is routed through our API gateway. We can even prove this by adding the -v option to the cURL command to display the headers returned. These headers are added by the Kong API gateway:

< X-Kong-Upstream-Latency: 1
< X-Kong-Proxy-Latency: 0
< Via: kong/1.3.0

Stop the services again to add some lines to the configuration.

docker-compose down