programatik29 / axum-server

High level server designed to be used with axum framework.
MIT License
174 stars 60 forks source link

Hybrid server with axum+tonic #22

Closed pixelspark closed 2 years ago

pixelspark commented 2 years ago

I am trying to build a hybrid HTTP + gRPC server using Axum and Tonic following this example. I am struggling a bit however on how to combine this with axum-server to have both over TLS:

let app = /* axum app */;
let grpc_service = tonic::transport::Server::builder().add_service(/* some gRPC service */).into_service();
let hybrid_make_service = hybrid(app.into_make_service(), grpc_service);

// This works fine
axum::Server::bind(&sc.bind_address).serve(hybrid_make_service).await?;

// This I can't get right...
axum_server::bind_rustls(sc.bind_address.to_string())
                .private_key_file(tls_key_path)
                .certificate_file(tls_cert_path)
                .serve(hybrid_make_service)
                .await?;

The issue seems to be that axum_server requires a 'Service' whereas the regular 'bind' accepts some sort of 'IntoService' (and the hybrid function, as well as grpc_service, create this). Any ideas?

pixelspark commented 2 years ago

See also here: https://www.fpcomplete.com/blog/axum-hyper-tonic-tower-part4/

programatik29 commented 2 years ago

You need to pass a Service that is Clone.

All Services provided by axum are Clone by default.

RouterService implements Clone if service wrapped in it implements it. So you need to make sure wrapped gRPC service is Clone.

programatik29 commented 2 years ago

I think it is a good idea to add HTTP+gRPC example.

programatik29 commented 2 years ago

Let me know if this helps.

pixelspark commented 2 years ago

Let me know if this helps.

Absolutely, this works like a charm! Thanks so much (and indeed, good idea to add this example, should probably end up in this repo?)

itsyaasir commented 2 years ago

Hey, I am implementing the same http+grpc , and I can see the example has been deleted , can I get it from anywhere ? Thank you

programatik29 commented 2 years ago

@itsyaasir I removed it because it wasn't up to date. I will write a new example for the new api and let you know when its done.

itsyaasir commented 2 years ago

@itsyaasir I removed it because it wasn't up to date. I will write a new example for the new api and let you know when its done.

Thank you , I appreciate that

programatik29 commented 2 years ago

@itsyaasir Let me know if this example works for you.

itsyaasir commented 2 years ago

@itsyaasir Let me know if this example works for you.

Absolutely, it worked, I got the general idea of the implementation. Thank you