Created
May 17, 2024 06:53
-
-
Save HuakunShen/aa3e2954960354e24bb0a15e8f9898f7 to your computer and use it in GitHub Desktop.
axum-tonic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "axum-tonic" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
tonic = "0.11" | |
prost = "0.12" | |
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } | |
tonic-web = "0.11.0" | |
axum = "0.6.20" | |
axum-server = "0.6.0" | |
http = "1.1.0" | |
tower = "0.4.13" | |
[build-dependencies] | |
tonic-build = "0.11" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
package greeter; | |
service Greeter { | |
rpc Greet (GreetRequest) returns (GreetReply); | |
} | |
message GreetRequest { | |
string name = 1; | |
} | |
message GreetReply { | |
string answer = 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use axum::routing::get; | |
use axum::ServiceExt; | |
use axum::{body::Body, Router}; | |
use std::error::Error; | |
use std::net::SocketAddr; | |
use tonic::{transport::Server as tonic_server, Status}; | |
use greeter::{ | |
greeter_server::{Greeter, GreeterServer}, | |
GreetReply, GreetRequest, | |
}; | |
mod greeter { | |
tonic::include_proto!("greeter"); | |
} | |
#[derive(Debug, Default)] | |
struct GreeterStruct {} | |
#[tonic::async_trait] | |
impl Greeter for GreeterStruct { | |
async fn greet( | |
&self, | |
req: tonic::Request<GreetRequest>, | |
) -> Result<tonic::Response<GreetReply>, tonic::Status> { | |
let name = req.into_inner().name; | |
println!("In greet(), name: {}", name); | |
let reply = GreetReply { | |
answer: format!("Hello {name}!").into(), | |
}; | |
Ok(tonic::Response::new(reply)) | |
} | |
} | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn Error>> { | |
let greeter_service = GreeterServer::new(GreeterStruct::default()); | |
let grpc_web_greeter: tonic_web::CorsGrpcWeb<GreeterServer<GreeterStruct>> = | |
tonic_web::enable(greeter_service.clone()); | |
let axum_addr: SocketAddr = "[::]:50051".parse()?; | |
// let service = tower::service_fn(|request: Request| async { | |
// Ok::<_, std::convert::Infallible>(http::Response::new(Body::empty())) | |
// }); | |
let gretter = GreeterStruct::default(); | |
let grpc_router = tonic_server::builder() | |
.add_service(GreeterServer::new(GreeterStruct::default())) | |
.into_router() | |
.route("/", get(web_root)); | |
// let web_app = axum::Router::new() | |
// .route( | |
// "/greeter.Greeter/*rpc", | |
// axum::routing::any_service(grpc_web_greeter.clone()), | |
// ) | |
// // .route("/greeter.Greeter/*rpc", axum::routing::any_service(service)) | |
// .route("/", get(web_root)); | |
// serve web_app router | |
axum::Server::bind(&axum_addr) | |
// .serve(web_app.into_make_service()) | |
.serve(grpc_router.into_make_service()) | |
.await?; | |
Ok(()) | |
} | |
async fn web_root() -> &'static str { | |
"Hello World!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment