From 6ca78c66f9d976bbc4d472057b903243fcfb4d8b Mon Sep 17 00:00:00 2001 From: Alec Thilenius Date: Tue, 25 Apr 2023 11:17:22 -0700 Subject: [PATCH] Add content-type json to response headers --- axum-connect-examples/src/main.rs | 25 +++---------------------- axum-connect/Cargo.toml | 2 +- axum-connect/src/handler.rs | 5 ++++- axum-connect/src/response.rs | 9 ++++++++- 4 files changed, 16 insertions(+), 25 deletions(-) diff --git a/axum-connect-examples/src/main.rs b/axum-connect-examples/src/main.rs index eee1e2e..41aa4df 100644 --- a/axum-connect-examples/src/main.rs +++ b/axum-connect-examples/src/main.rs @@ -1,7 +1,7 @@ use std::net::SocketAddr; use axum::{extract::Host, Router}; -use axum_connect::{error::RpcError, prelude::*}; +use axum_connect::prelude::*; use proto::hello::{HelloRequest, HelloResponse, HelloWorldService}; mod proto { @@ -13,14 +13,10 @@ async fn main() { // Build our application with a route. Note the `rpc` method which was added by `axum-connect`. // It expect a service method handler, wrapped in it's respective type. The handler (below) is // just a normal Rust function. Just like Axum, it also supports extractors! - let app = Router::new() - .rpc(HelloWorldService::say_hello(say_hello_success)) - .rpc(HelloWorldService::say_hello(say_hello_error)) - .rpc(HelloWorldService::say_hello(say_hello_result)) - .rpc(HelloWorldService::say_hello(say_hello_error_code)); + let app = Router::new().rpc(HelloWorldService::say_hello(say_hello_success)); // Axum boilerplate to start the server. - let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let addr = SocketAddr::from(([127, 0, 0, 1], 3030)); println!("listening on http://{}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) @@ -37,18 +33,3 @@ async fn say_hello_success(Host(host): Host, request: HelloRequest) -> HelloResp special_fields: Default::default(), } } - -async fn say_hello_error(_request: HelloRequest) -> RpcError { - RpcError::new(RpcErrorCode::Unimplemented, "Not implemented".to_string()) -} - -async fn say_hello_error_code(_request: HelloRequest) -> RpcErrorCode { - RpcErrorCode::Unimplemented -} - -async fn say_hello_result(_request: HelloRequest) -> RpcResult { - Ok(HelloResponse { - message: "Hello World!".to_string(), - special_fields: Default::default(), - }) -} diff --git a/axum-connect/Cargo.toml b/axum-connect/Cargo.toml index 0d9e79f..87f5b94 100644 --- a/axum-connect/Cargo.toml +++ b/axum-connect/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum-connect" -version = "0.1.1" +version = "0.1.2" edition = "2021" categories = ["rpc", "connect-web", "axum", "http"] description = "Connect-Web RPC for Axum" diff --git a/axum-connect/src/handler.rs b/axum-connect/src/handler.rs index ad996a3..56bced7 100644 --- a/axum-connect/src/handler.rs +++ b/axum-connect/src/handler.rs @@ -19,6 +19,9 @@ pub trait HandlerFuture: Clone + Send + Sized + 'stati fn call(self, req: Request, state: S) -> Self::Future; } +// This is a single expanded version of the macro below. It's left here for ease of reading and +// understanding the macro, as well as development. +// ```rust // #[allow(unused_parens, non_snake_case, unused_mut)] // impl HandlerFuture for F // where @@ -72,7 +75,7 @@ pub trait HandlerFuture: Clone + Send + Sized + 'stati // }) // } // } - +// ``` macro_rules! impl_handler { ( [$($ty:ident),*] diff --git a/axum-connect/src/response.rs b/axum-connect/src/response.rs index 24a4e18..080bd45 100644 --- a/axum-connect/src/response.rs +++ b/axum-connect/src/response.rs @@ -29,7 +29,14 @@ where } }; - let (parts, _) = self.parts.into_parts(); + let (mut parts, _) = self.parts.into_parts(); + + // Add Content-Type JSON header to parts. + parts.headers.insert( + "Content-Type", + "application/json".parse().expect("Wrong MIME type"), + ); + (parts, rpc_call_response).into_response() } }