forked from lavina/lavina
1
0
Fork 0

Compare commits

..

No commits in common. "9bd00a57895f9321eb718558a557810077583e93" and "7bc2216ebc00c4c59529e18ee787b180cbb46268" have entirely different histories.

2 changed files with 28 additions and 26 deletions

View File

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

View File

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