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 using the Admin API, which is available on port 8001.
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 use the following command to create a service in Kong:
curl -X POST \
http://localhost:8001/services \
-H 'Content-Type: application/json' \
-d '{
"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 command 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:
curl -X POST \
http://localhost:8001/services/rover-api/routes \
-H 'Content-Type: application/json' \
-d '{
"paths": ["/rovers"],
"name": "rover_api_route",
"methods": ["GET","POST","PUT","DELETE"]
}'
Call the API
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.