Skip to content

sunsided/http-grpc-cohosting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tonic gRPC and Axum HTTP/1.1+2 co-hosting in Rust

An attempt at hosting HTTP/1.1, HTTP/2 and gRPC on the same port(s).

Multiple Hyper servers are spawned on different endpoints to showcase the use of binding to different IP addresses and ports while reusing the same server components. A Hyper service is used to switch the incoming traffic based on the content-type header and if application/grpc is detected, traffic is forwarded to the Tonic server; all other cases forward to Axum. This allows for transparent use of HTTP/1.1 and HTTP/2 (prior knowledge), as well as ALPN on the TLS-enabled ports. On Unixoids, Unix Domain Sockets are supported as well.

This project uses:

  • Hyper as the server for HTTP/1 and HTTP/2 support.
  • Axum as the HTTP server.
  • Tonic as the gRPC server.

Curl

non-TLS

curl -v http://127.0.0.1:36849/
curl -v http://127.1.0.1:36849/
curl --http2-prior-knowledge --insecure -vv http://127.0.0.1:36849/

Unix Domain Sockets

curl -v --unix-socket /tmp/cohosting.sock http://localhost:36849/

TLS with ALPN

curl --insecure -v https://127.0.0.1:36850/
curl --http1.1 --insecure -vv https://127.0.0.1:36850/
curl --http2 --insecure -vv https://127.0.0.1:36850/

nghttp (HTTP/2)

nghttp -v http://127.0.0.1:36849
nghttp -y -v https://127.0.0.1:36850

gRPC testing

Use gRPC reflection to introspect the service:

grpcurl --plaintext --use-reflection 127.0.0.1:36849 list
grpcurl --insecure --use-reflection 127.0.0.1:36850 list

Send a test request:

grpcurl --plaintext --use-reflection -d '{ "message": "World" }' 127.0.0.1:36849 example.YourService/YourMethod
grpcurl --insecure --use-reflection -d '{ "message": "World" }' 127.0.0.1:36850 example.YourService/YourMethod

Unix Domain Sockets

For UDS to work with gRPC, the :authority header needs to be sent. In grpcurl, the --authority=xyz flag is used for that:

grpcurl --unix --plaintext --use-reflection --authority localhost -d '{ "message": "World" }' /tmp/cohosting.sock example.YourService/YourMethod

Recommended reads

The Combining Axum, Hyper, Tonic, and Tower for hybrid web/gRPC apps series:

About

Hosting Axum HTTP and Tonic gRPC on the same Hyper server

Topics

Resources

Stars

Watchers

Forks

Languages