forked from lavina/lavina
clean up http.rs a little
This commit is contained in:
parent
4ff09ea05f
commit
72f5010988
45
src/http.rs
45
src/http.rs
|
@ -111,14 +111,7 @@ async fn endpoint_create_player(
|
|||
) -> Result<Response<Full<Bytes>>> {
|
||||
let str = request.collect().await?.to_bytes();
|
||||
let Ok(res) = serde_json::from_slice::<CreatePlayerRequest>(&str[..]) else {
|
||||
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 Ok(response);
|
||||
return Ok(malformed_request());
|
||||
};
|
||||
storage.create_user(&res.name).await?;
|
||||
log::info!("Player {} created", res.name);
|
||||
|
@ -129,18 +122,11 @@ async fn endpoint_create_player(
|
|||
|
||||
async fn endpoint_set_password(
|
||||
request: Request<hyper::body::Incoming>,
|
||||
mut storage: Storage,
|
||||
storage: Storage,
|
||||
) -> Result<Response<Full<Bytes>>> {
|
||||
let str = request.collect().await?.to_bytes();
|
||||
let Ok(res) = serde_json::from_slice::<ChangePasswordRequest>(&str[..]) else {
|
||||
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 Ok(response);
|
||||
return Ok(malformed_request());
|
||||
};
|
||||
let verdict = Authenticator::new(&storage).set_password(&res.player_name, &res.password).await?;
|
||||
match verdict {
|
||||
|
@ -173,19 +159,28 @@ pub fn not_found() -> Response<Full<Bytes>> {
|
|||
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 {
|
||||
fn or5xx(self) -> Response<Full<Bytes>>;
|
||||
}
|
||||
impl Or5xx for Result<Response<Full<Bytes>>> {
|
||||
fn or5xx(self) -> Response<Full<Bytes>> {
|
||||
match self {
|
||||
Ok(e) => e,
|
||||
Err(e) => {
|
||||
let mut response = Response::new(Full::new(e.to_string().into()));
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
response
|
||||
}
|
||||
}
|
||||
self.unwrap_or_else(|e| {
|
||||
let mut response = Response::new(Full::new(e.to_string().into()));
|
||||
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||
response
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue