mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Update to latest versions of hyper and http-body (#1882)
Co-authored-by: Michael Scofield <[email protected]> Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Michael Scofield
Jonas Platte
parent
2f4720907a
commit
43b14a5f02
@@ -4,79 +4,84 @@
|
||||
//! cargo run -p example-rest-grpc-multiplex
|
||||
//! ```
|
||||
|
||||
use self::multiplex_service::MultiplexService;
|
||||
use axum::{routing::get, Router};
|
||||
use proto::{
|
||||
greeter_server::{Greeter, GreeterServer},
|
||||
HelloReply, HelloRequest,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
use tonic::{Response as TonicResponse, Status};
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod multiplex_service;
|
||||
|
||||
mod proto {
|
||||
tonic::include_proto!("helloworld");
|
||||
|
||||
pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
|
||||
tonic::include_file_descriptor_set!("helloworld_descriptor");
|
||||
// TODO
|
||||
fn main() {
|
||||
eprint!("this example has not yet been updated to hyper 1.0");
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct GrpcServiceImpl {}
|
||||
// use self::multiplex_service::MultiplexService;
|
||||
// use axum::{routing::get, Router};
|
||||
// use proto::{
|
||||
// greeter_server::{Greeter, GreeterServer},
|
||||
// HelloReply, HelloRequest,
|
||||
// };
|
||||
// use std::net::SocketAddr;
|
||||
// use tonic::{Response as TonicResponse, Status};
|
||||
// use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Greeter for GrpcServiceImpl {
|
||||
async fn say_hello(
|
||||
&self,
|
||||
request: tonic::Request<HelloRequest>,
|
||||
) -> Result<TonicResponse<HelloReply>, Status> {
|
||||
tracing::info!("Got a request from {:?}", request.remote_addr());
|
||||
// mod multiplex_service;
|
||||
|
||||
let reply = HelloReply {
|
||||
message: format!("Hello {}!", request.into_inner().name),
|
||||
};
|
||||
// mod proto {
|
||||
// tonic::include_proto!("helloworld");
|
||||
|
||||
Ok(TonicResponse::new(reply))
|
||||
}
|
||||
}
|
||||
// pub(crate) const FILE_DESCRIPTOR_SET: &[u8] =
|
||||
// tonic::include_file_descriptor_set!("helloworld_descriptor");
|
||||
// }
|
||||
|
||||
async fn web_root() -> &'static str {
|
||||
"Hello, World!"
|
||||
}
|
||||
// #[derive(Default)]
|
||||
// struct GrpcServiceImpl {}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// initialize tracing
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "example_rest_grpc_multiplex=debug".into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
// #[tonic::async_trait]
|
||||
// impl Greeter for GrpcServiceImpl {
|
||||
// async fn say_hello(
|
||||
// &self,
|
||||
// request: tonic::Request<HelloRequest>,
|
||||
// ) -> Result<TonicResponse<HelloReply>, Status> {
|
||||
// tracing::info!("Got a request from {:?}", request.remote_addr());
|
||||
|
||||
// build the rest service
|
||||
let rest = Router::new().route("/", get(web_root));
|
||||
// let reply = HelloReply {
|
||||
// message: format!("Hello {}!", request.into_inner().name),
|
||||
// };
|
||||
|
||||
// build the grpc service
|
||||
let reflection_service = tonic_reflection::server::Builder::configure()
|
||||
.register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET)
|
||||
.build()
|
||||
.unwrap();
|
||||
let grpc = tonic::transport::Server::builder()
|
||||
.add_service(reflection_service)
|
||||
.add_service(GreeterServer::new(GrpcServiceImpl::default()))
|
||||
.into_service();
|
||||
// Ok(TonicResponse::new(reply))
|
||||
// }
|
||||
// }
|
||||
|
||||
// combine them into one service
|
||||
let service = MultiplexService::new(rest, grpc);
|
||||
// async fn web_root() -> &'static str {
|
||||
// "Hello, World!"
|
||||
// }
|
||||
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
tracing::debug!("listening on {addr}");
|
||||
hyper::Server::bind(&addr)
|
||||
.serve(tower::make::Shared::new(service))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
// #[tokio::main]
|
||||
// async fn main() {
|
||||
// // initialize tracing
|
||||
// tracing_subscriber::registry()
|
||||
// .with(
|
||||
// tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
// .unwrap_or_else(|_| "example_rest_grpc_multiplex=debug".into()),
|
||||
// )
|
||||
// .with(tracing_subscriber::fmt::layer())
|
||||
// .init();
|
||||
|
||||
// // build the rest service
|
||||
// let rest = Router::new().route("/", get(web_root));
|
||||
|
||||
// // build the grpc service
|
||||
// let reflection_service = tonic_reflection::server::Builder::configure()
|
||||
// .register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET)
|
||||
// .build()
|
||||
// .unwrap();
|
||||
// let grpc = tonic::transport::Server::builder()
|
||||
// .add_service(reflection_service)
|
||||
// .add_service(GreeterServer::new(GrpcServiceImpl::default()))
|
||||
// .into_service();
|
||||
|
||||
// // combine them into one service
|
||||
// let service = MultiplexService::new(rest, grpc);
|
||||
|
||||
// let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
// tracing::debug!("listening on {}", addr);
|
||||
// hyper::Server::bind(&addr)
|
||||
// .serve(tower::make::Shared::new(service))
|
||||
// .await
|
||||
// .unwrap();
|
||||
// }
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::Request,
|
||||
http::header::CONTENT_TYPE,
|
||||
response::{IntoResponse, Response},
|
||||
|
||||
Reference in New Issue
Block a user