This repo is an example of how to create an NGINX proxy as a gateway to your micro services.
There's a detailed blog post about it.
The important thing is that you can run tests to verify that the gateway routing is done correctly, meaning a request is routed to a specified service with a specified path.
For example, you can verify that an HTTP call to http://api.mycompany.com/login is routed to your authentication service with the /login path.
Make sure docker and docker-compose are installed on your machine.
Clone this repository:
$ git clone [email protected]:nanit/api-gateway-example.git
And run the tests:
$ cd api-gateway-example && make test
All tests should pass which means that:
- A request to http://your.gateway.com/login is routed to the authentication service with the /login path
- A request to http://your.gateway.com/messages is routed to the inbox service with the /messages path
-
spec/app/spec/expected_routes.yml - the route specs. Each route has an expected destination service name and an expected destination path.
-
app/services/<service_name>.conf
- per-service routes configuration. -
app/nginx.conf - the main nginx configuration file.
-
docker-compose.yml - add new services here.
You should only change these 4 to fit your specific needs. The rest of the files may remain untouched.
-
Add the route to spec/app/spec/expected_routes.yml
-
Run
make test
in the root directory and see the tests fail. -
Add the proper NGINX configuration.
-
Run
make test
again in the root directory and (hopefully) see the tests pass.
-
Add the service to docker-compose.yml. Make sure the SERVICE_NAME environment variable is set properly, and that the gateway is linked to it.
-
Add your service specs to spec/app/spec/expected_routes.yml
-
Run
make test
in the root directory to see the specs fail. -
Add
app/services/new_service.conf
with the proper routing to your new service. -
Run
make test
in the root directory again and (hopefully again) see the tests pass.