forked from lavina/lavina
fix
This commit is contained in:
parent
7bc2216ebc
commit
03eb861131
46
src/http.rs
46
src/http.rs
|
@ -86,9 +86,9 @@ async fn route(
|
||||||
(&Method::GET, "/metrics") => endpoint_metrics(registry),
|
(&Method::GET, "/metrics") => endpoint_metrics(registry),
|
||||||
(&Method::GET, "/rooms") => endpoint_rooms(core.rooms).await,
|
(&Method::GET, "/rooms") => endpoint_rooms(core.rooms).await,
|
||||||
(&Method::POST, paths::CREATE_PLAYER) => endpoint_create_player(request, storage).await.or5xx(),
|
(&Method::POST, paths::CREATE_PLAYER) => endpoint_create_player(request, storage).await.or5xx(),
|
||||||
(&Method::POST, paths::STOP_PLAYER) => endpoint_stop_player(request, storage).await.or5xx(),
|
(&Method::POST, paths::STOP_PLAYER) => endpoint_stop_player(request, core.players).await.or5xx(),
|
||||||
(&Method::POST, paths::SET_PASSWORD) => endpoint_set_password(request, storage).await.or5xx(),
|
(&Method::POST, paths::SET_PASSWORD) => endpoint_set_password(request, storage).await.or5xx(),
|
||||||
_ => not_found(),
|
_ => endpoint_not_found(),
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
@ -131,27 +131,13 @@ async fn endpoint_stop_player(
|
||||||
return Ok(malformed_request());
|
return Ok(malformed_request());
|
||||||
};
|
};
|
||||||
let Ok(player_id) = PlayerId::from(res.name) else {
|
let Ok(player_id) = PlayerId::from(res.name) else {
|
||||||
let payload = ErrorResponse {
|
return Ok(player_not_found());
|
||||||
code: errors::PLAYER_NOT_FOUND,
|
|
||||||
message: "No such player exists",
|
|
||||||
}
|
|
||||||
.to_body();
|
|
||||||
let mut response = Response::new(payload);
|
|
||||||
*response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
|
|
||||||
return Ok(response);
|
|
||||||
};
|
};
|
||||||
let Some(()) = players.stop_player(&player_id).await? else {
|
let Some(()) = players.stop_player(&player_id).await? else {
|
||||||
let payload = ErrorResponse {
|
return Ok(player_not_found());
|
||||||
code: errors::PLAYER_NOT_FOUND,
|
|
||||||
message: "No such player exists",
|
|
||||||
}
|
|
||||||
.to_body();
|
|
||||||
let mut response = Response::new(payload);
|
|
||||||
*response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
|
|
||||||
return Ok(response);
|
|
||||||
};
|
};
|
||||||
let mut response = Response::new(Full::<Bytes>::default());
|
let mut response = Response::new(Full::<Bytes>::default());
|
||||||
*response.status_mut() = StatusCode::CREATED;
|
*response.status_mut() = StatusCode::NO_CONTENT;
|
||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,14 +153,7 @@ async fn endpoint_set_password(
|
||||||
match verdict {
|
match verdict {
|
||||||
UpdatePasswordResult::PasswordUpdated => {}
|
UpdatePasswordResult::PasswordUpdated => {}
|
||||||
UpdatePasswordResult::UserNotFound => {
|
UpdatePasswordResult::UserNotFound => {
|
||||||
let payload = ErrorResponse {
|
return Ok(player_not_found());
|
||||||
code: errors::PLAYER_NOT_FOUND,
|
|
||||||
message: "No such player exists",
|
|
||||||
}
|
|
||||||
.to_body();
|
|
||||||
let mut response = Response::new(payload);
|
|
||||||
*response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut response = Response::new(Full::<Bytes>::default());
|
let mut response = Response::new(Full::<Bytes>::default());
|
||||||
|
@ -182,7 +161,7 @@ async fn endpoint_set_password(
|
||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn not_found() -> Response<Full<Bytes>> {
|
fn endpoint_not_found() -> Response<Full<Bytes>> {
|
||||||
let payload = ErrorResponse {
|
let payload = ErrorResponse {
|
||||||
code: errors::INVALID_PATH,
|
code: errors::INVALID_PATH,
|
||||||
message: "The path does not exist",
|
message: "The path does not exist",
|
||||||
|
@ -194,6 +173,17 @@ pub fn not_found() -> Response<Full<Bytes>> {
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn player_not_found() -> Response<Full<Bytes>> {
|
||||||
|
let payload = ErrorResponse {
|
||||||
|
code: errors::PLAYER_NOT_FOUND,
|
||||||
|
message: "No such player exists",
|
||||||
|
}
|
||||||
|
.to_body();
|
||||||
|
let mut response = Response::new(payload);
|
||||||
|
*response.status_mut() = StatusCode::UNPROCESSABLE_ENTITY;
|
||||||
|
response
|
||||||
|
}
|
||||||
|
|
||||||
fn malformed_request() -> Response<Full<Bytes>> {
|
fn malformed_request() -> Response<Full<Bytes>> {
|
||||||
let payload = ErrorResponse {
|
let payload = ErrorResponse {
|
||||||
code: errors::MALFORMED_REQUEST,
|
code: errors::MALFORMED_REQUEST,
|
||||||
|
|
Loading…
Reference in New Issue