Add content-type json to response headers

This commit is contained in:
Alec Thilenius 2023-04-25 11:17:22 -07:00
parent 7bc2b9f6f4
commit 6ca78c66f9
4 changed files with 16 additions and 25 deletions

View file

@ -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<HelloResponse> {
Ok(HelloResponse {
message: "Hello World!".to_string(),
special_fields: Default::default(),
})
}

View file

@ -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"

View file

@ -19,6 +19,9 @@ pub trait HandlerFuture<TReq, TRes, Res, T, S, B>: Clone + Send + Sized + 'stati
fn call(self, req: Request<B>, 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<TReq, TRes, Res, F, Fut, S, B, T1> HandlerFuture<TReq, TRes, Res, (T1, TReq), S, B> for F
// where
@ -72,7 +75,7 @@ pub trait HandlerFuture<TReq, TRes, Res, T, S, B>: Clone + Send + Sized + 'stati
// })
// }
// }
// ```
macro_rules! impl_handler {
(
[$($ty:ident),*]

View file

@ -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()
}
}