Skip to content

Latest commit

 

History

History
75 lines (51 loc) · 2.04 KB

README.md

File metadata and controls

75 lines (51 loc) · 2.04 KB

Actix + Tonic

A sample project that shows how set up an Actix Web server that communicates with a "backend" gRPC server via Tonic.

Actix Web

The initial code for the Actix Web part of this project was taken directly from the Getting Started documentation page.

Tonic

The initial code for Tonic comes from the Getting Started hello world tutorial.

Actix + Tonic

The example is completed by putting a Tonic client in the Actix Web server.

The Actix Web server is started in another thread, based on the instructions given in the Actix server documentation. The Tonic client is passed to the server as application data, see the Data struct documentation.

The Tonic client can be used concurrently. To achieve this we simply clone the application state containing the client and use the clone as described in the client documentation.

Running the example

To run the example, you need three terminals (or whatever your preference is to run three commands at the same time):

  • Backend Server - Tonic gRPC Server
  • Frontend Server - Actix Web HTTP Server
  • HTTP Client - Curl

We start them in that order.

Start the backend (HTTP) server:

cargo run --bin backend-server-tonic

Start the frontend (gRPC) server:

cargo run --bin frontend-server-actix

Finally, make a request to the frontend (HTTP) server with curl:

curl -X POST -d "My name" 127.0.0.1:8080/echo

You should see the response:

Hello My name!

Of course, this doesn't show that this request made its way all the way up to the Tonic server, but you can check the logs from the frontend and backend servers to verify that.

License

This project is licensed under the MIT license.