Implement `IntoResponse` for `UsersResponse`

- Manually serialze the UsersResponse to json and set the correct
  content type
main
Avery 7 days ago
parent e82c40f64c
commit 4ddcecb650
Signed by: Avery
GPG Key ID: 4E53F4CB69B2CC8D

1
Cargo.lock generated

@ -1116,6 +1116,7 @@ dependencies = [
"log",
"rsa",
"serde",
"serde_json",
"sha2",
"tokio",
"tower-layer",

@ -9,6 +9,7 @@ env_logger = "0.11.8"
log = "0.4.27"
rsa = "0.9.8"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
sha2 = "0.10.8"
tokio = { version = "1.44.2", features = ["full"] }
tower-layer = "0.3.3"

@ -46,20 +46,20 @@ async fn fallback(uri: Uri) -> (StatusCode, String) {
pub struct TraceLayer;
impl<S> Layer<S> for TraceLayer {
type Service = LogService<S>;
type Service = TraceService<S>;
fn layer(&self, service: S) -> Self::Service {
LogService { service }
TraceService { service }
}
}
// This service implements the Log behavior
#[derive(Clone)]
pub struct LogService<S> {
pub struct TraceService<S> {
service: S,
}
impl<S, Request> Service<Request> for LogService<S>
impl<S, Request> Service<Request> for TraceService<S>
where
S: Service<Request>,
Request: fmt::Debug,

@ -1,4 +1,7 @@
use axum::{Json, extract::Path};
use axum::{
extract::Path,
response::{IntoResponse, Response},
};
use log::debug;
use serde::Serialize;
@ -13,14 +16,23 @@ pub struct UsersResponse {
id: String,
}
pub async fn users_get(Path(user): Path<String>) -> Json<UsersResponse> {
pub async fn users_get(Path(user): Path<String>) -> UsersResponse {
debug!("Got request for actor: {user}");
assert_eq!(user, USER);
Json(UsersResponse {
UsersResponse {
context: "https://www.w3.org/ns/activitystreams".to_string(),
r#type: "Person".to_string(),
name: "Sally Smith".to_string(),
id: "https://testhost.com/users/test".to_string(),
})
}
}
impl IntoResponse for UsersResponse {
fn into_response(self) -> Response {
Response::builder()
.header("content-type", "application/activity+json; charset=utf-8")
.body(serde_json::to_string(&self).unwrap().into())
.unwrap()
}
}

Loading…
Cancel
Save