clean up http.rs a little

This commit is contained in:
Nikita Vilunov 2024-04-26 12:28:13 +02:00
parent 4ff09ea05f
commit 72f5010988
1 changed files with 20 additions and 25 deletions

View File

@ -111,14 +111,7 @@ async fn endpoint_create_player(
) -> Result<Response<Full<Bytes>>> { ) -> Result<Response<Full<Bytes>>> {
let str = request.collect().await?.to_bytes(); let str = request.collect().await?.to_bytes();
let Ok(res) = serde_json::from_slice::<CreatePlayerRequest>(&str[..]) else { let Ok(res) = serde_json::from_slice::<CreatePlayerRequest>(&str[..]) else {
let payload = ErrorResponse { return Ok(malformed_request());
code: errors::MALFORMED_REQUEST,
message: "The request payload contains incorrect JSON value",
}
.to_body();
let mut response = Response::new(payload);
*response.status_mut() = StatusCode::BAD_REQUEST;
return Ok(response);
}; };
storage.create_user(&res.name).await?; storage.create_user(&res.name).await?;
log::info!("Player {} created", res.name); log::info!("Player {} created", res.name);
@ -129,18 +122,11 @@ async fn endpoint_create_player(
async fn endpoint_set_password( async fn endpoint_set_password(
request: Request<hyper::body::Incoming>, request: Request<hyper::body::Incoming>,
mut storage: Storage, storage: Storage,
) -> Result<Response<Full<Bytes>>> { ) -> Result<Response<Full<Bytes>>> {
let str = request.collect().await?.to_bytes(); let str = request.collect().await?.to_bytes();
let Ok(res) = serde_json::from_slice::<ChangePasswordRequest>(&str[..]) else { let Ok(res) = serde_json::from_slice::<ChangePasswordRequest>(&str[..]) else {
let payload = ErrorResponse { return Ok(malformed_request());
code: errors::MALFORMED_REQUEST,
message: "The request payload contains incorrect JSON value",
}
.to_body();
let mut response = Response::new(payload);
*response.status_mut() = StatusCode::BAD_REQUEST;
return Ok(response);
}; };
let verdict = Authenticator::new(&storage).set_password(&res.player_name, &res.password).await?; let verdict = Authenticator::new(&storage).set_password(&res.player_name, &res.password).await?;
match verdict { match verdict {
@ -173,19 +159,28 @@ pub fn not_found() -> Response<Full<Bytes>> {
response response
} }
fn malformed_request() -> Response<Full<Bytes>> {
let payload = ErrorResponse {
code: errors::MALFORMED_REQUEST,
message: "The request payload contains incorrect JSON value",
}
.to_body();
let mut response = Response::new(payload);
*response.status_mut() = StatusCode::BAD_REQUEST;
return response;
}
trait Or5xx { trait Or5xx {
fn or5xx(self) -> Response<Full<Bytes>>; fn or5xx(self) -> Response<Full<Bytes>>;
} }
impl Or5xx for Result<Response<Full<Bytes>>> { impl Or5xx for Result<Response<Full<Bytes>>> {
fn or5xx(self) -> Response<Full<Bytes>> { fn or5xx(self) -> Response<Full<Bytes>> {
match self { self.unwrap_or_else(|e| {
Ok(e) => e,
Err(e) => {
let mut response = Response::new(Full::new(e.to_string().into())); let mut response = Response::new(Full::new(e.to_string().into()));
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
response response
} })
}
} }
} }