forked from lavina/lavina
1
0
Fork 0

Compare commits

..

2 Commits

Author SHA1 Message Date
Nikita Vilunov 9bd00a5789 telemetry 2024-04-29 02:39:01 +02:00
Nikita Vilunov 03eb861131 fix 2024-04-29 02:25:17 +02:00
2 changed files with 26 additions and 28 deletions

View File

@ -276,11 +276,13 @@ impl PlayerRegistry {
Ok(())
}
#[tracing::instrument(skip(self), name = "PlayerRegistry::get_player")]
pub async fn get_player(&self, id: &PlayerId) -> Option<PlayerHandle> {
let inner = self.0.read().await;
inner.players.get(id).map(|(handle, _)| handle.clone())
}
#[tracing::instrument(skip(self), name = "PlayerRegistry::stop_player")]
pub async fn stop_player(&self, id: &PlayerId) -> Result<Option<()>> {
let mut inner = self.0.write().await;
if let Some((handle, fiber)) = inner.players.remove(id) {
@ -294,6 +296,7 @@ impl PlayerRegistry {
}
}
#[tracing::instrument(skip(self), name = "PlayerRegistry::get_or_launch_player")]
pub async fn get_or_launch_player(&mut self, id: &PlayerId) -> PlayerHandle {
let inner = self.0.read().await;
if let Some((handle, _)) = inner.players.get(id) {
@ -318,6 +321,7 @@ impl PlayerRegistry {
}
}
#[tracing::instrument(skip(self), name = "PlayerRegistry::connect_to_player")]
pub async fn connect_to_player(&mut self, id: &PlayerId) -> PlayerConnection {
let player_handle = self.get_or_launch_player(id).await;
player_handle.subscribe().await

View File

@ -86,9 +86,9 @@ async fn route(
(&Method::GET, "/metrics") => endpoint_metrics(registry),
(&Method::GET, "/rooms") => endpoint_rooms(core.rooms).await,
(&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(),
_ => not_found(),
_ => endpoint_not_found(),
};
Ok(res)
}
@ -100,6 +100,7 @@ fn endpoint_metrics(registry: MetricsRegistry) -> Response<Full<Bytes>> {
Response::new(Full::new(Bytes::from(buffer)))
}
#[tracing::instrument(skip_all)]
async fn endpoint_rooms(rooms: RoomRegistry) -> Response<Full<Bytes>> {
// TODO introduce management API types independent from core-domain types
// TODO remove `Serialize` implementations from all core-domain types
@ -107,6 +108,7 @@ async fn endpoint_rooms(rooms: RoomRegistry) -> Response<Full<Bytes>> {
Response::new(room_list)
}
#[tracing::instrument(skip_all)]
async fn endpoint_create_player(
request: Request<hyper::body::Incoming>,
mut storage: Storage,
@ -122,6 +124,7 @@ async fn endpoint_create_player(
Ok(response)
}
#[tracing::instrument(skip_all)]
async fn endpoint_stop_player(
request: Request<hyper::body::Incoming>,
players: PlayerRegistry,
@ -131,30 +134,17 @@ async fn endpoint_stop_player(
return Ok(malformed_request());
};
let Ok(player_id) = PlayerId::from(res.name) else {
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;
return Ok(response);
return Ok(player_not_found());
};
let Some(()) = players.stop_player(&player_id).await? else {
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;
return Ok(response);
return Ok(player_not_found());
};
let mut response = Response::new(Full::<Bytes>::default());
*response.status_mut() = StatusCode::CREATED;
*response.status_mut() = StatusCode::NO_CONTENT;
Ok(response)
}
#[tracing::instrument(skip_all)]
async fn endpoint_set_password(
request: Request<hyper::body::Incoming>,
storage: Storage,
@ -167,14 +157,7 @@ async fn endpoint_set_password(
match verdict {
UpdatePasswordResult::PasswordUpdated => {}
UpdatePasswordResult::UserNotFound => {
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;
return Ok(response);
return Ok(player_not_found());
}
}
let mut response = Response::new(Full::<Bytes>::default());
@ -182,7 +165,7 @@ async fn endpoint_set_password(
Ok(response)
}
pub fn not_found() -> Response<Full<Bytes>> {
fn endpoint_not_found() -> Response<Full<Bytes>> {
let payload = ErrorResponse {
code: errors::INVALID_PATH,
message: "The path does not exist",
@ -194,6 +177,17 @@ pub fn not_found() -> Response<Full<Bytes>> {
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>> {
let payload = ErrorResponse {
code: errors::MALFORMED_REQUEST,