diff --git a/editoast/src/views/infra/mod.rs b/editoast/src/views/infra/mod.rs index 03f9ec8244c..992025a108a 100644 --- a/editoast/src/views/infra/mod.rs +++ b/editoast/src/views/infra/mod.rs @@ -123,7 +123,13 @@ struct RefreshResponse { ) )] async fn refresh( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + infra_caches, + map_layers, + .. + }): State, Extension(auth): AuthenticationExt, Query(query_params): Query, ) -> Result> { @@ -135,11 +141,6 @@ async fn refresh( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let infra_caches = app_state.infra_caches.clone(); - let map_layers = app_state.map_layers.clone(); - // Use a transaction to give scope to infra list lock let RefreshQueryParams { force, @@ -160,7 +161,6 @@ async fn refresh( }; // Refresh each infras - let db_pool = db_pool; let mut infra_refreshed = vec![]; for mut infra in infras_list { @@ -201,7 +201,11 @@ struct InfraListResponse { ), )] async fn list( - app_state: State, + State(AppState { + db_pool, + osrdyne_client, + .. + }): State, Extension(auth): AuthenticationExt, pagination_params: Query, ) -> Result> { @@ -212,8 +216,6 @@ async fn list( if !authorized { return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let osrdyne_client = app_state.osrdyne_client.clone(); let settings = pagination_params .validate(1000)? @@ -295,7 +297,11 @@ struct InfraIdParam { ), )] async fn get( - app_state: State, + State(AppState { + db_pool, + osrdyne_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, ) -> Result> { @@ -307,9 +313,6 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let osrdyne_client = app_state.osrdyne_client.clone(); - let infra_id = infra.infra_id; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { InfraApiError::NotFound { infra_id } @@ -344,7 +347,7 @@ impl From for Changeset { ), )] async fn create( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(infra_form): Json, ) -> Result { @@ -381,7 +384,7 @@ struct CloneQuery { async fn clone( Extension(auth): AuthenticationExt, Path(params): Path, - db_pool: State, + State(db_pool): State, Query(CloneQuery { name }): Query, ) -> Result> { let authorized = auth @@ -421,7 +424,11 @@ async fn clone( ), )] async fn delete( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, infra: Path, ) -> Result { @@ -433,8 +440,6 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); let infra_id = infra.infra_id; if Infra::fast_delete_static(db_pool.get().await?, infra_id).await? { infra_caches.remove(&infra_id); @@ -468,7 +473,7 @@ impl From for Changeset { ), )] async fn put( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Path(infra): Path, Json(patch): Json, @@ -501,7 +506,11 @@ async fn put( ) )] async fn get_switch_types( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, ) -> Result>> { @@ -513,9 +522,7 @@ async fn get_switch_types( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let infra_caches = app_state.infra_caches.clone(); let infra = Infra::retrieve_or_fail(conn, infra.infra_id, || InfraApiError::NotFound { infra_id: infra.infra_id, @@ -546,7 +553,7 @@ async fn get_switch_types( async fn get_speed_limit_tags( Extension(auth): AuthenticationExt, Path(infra): Path, - db_pool: State, + State(db_pool): State, ) -> Result>> { let authorized = auth .check_roles([BuiltinRole::InfraRead].into()) @@ -590,7 +597,7 @@ async fn get_voltages( Extension(auth): AuthenticationExt, Path(infra): Path, Query(param): Query, - db_pool: State, + State(db_pool): State, ) -> Result>> { let authorized = auth .check_roles([BuiltinRole::InfraRead].into()) @@ -623,7 +630,7 @@ async fn get_voltages( ) )] async fn get_all_voltages( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result>> { let authorized = auth @@ -712,7 +719,11 @@ async fn unlock( ) )] async fn load( - app_state: State, + State(AppState { + db_pool, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(path): Path, ) -> Result { @@ -724,9 +735,6 @@ async fn load( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let core_client = app_state.core_client.clone(); - let infra_id = path.infra_id; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { InfraApiError::NotFound { infra_id } diff --git a/editoast/src/views/infra/pathfinding.rs b/editoast/src/views/infra/pathfinding.rs index d152f5fdac9..b4a77b80a0d 100644 --- a/editoast/src/views/infra/pathfinding.rs +++ b/editoast/src/views/infra/pathfinding.rs @@ -96,7 +96,11 @@ struct QueryParam { ) )] async fn pathfinding_view( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, Query(params): Query, @@ -110,9 +114,6 @@ async fn pathfinding_view( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); - // Parse and check input let infra_id = infra.infra_id; let number = params.number.unwrap_or(DEFAULT_NUMBER_OF_PATHS); diff --git a/editoast/src/views/infra/railjson.rs b/editoast/src/views/infra/railjson.rs index 2b62631590d..ad859718765 100644 --- a/editoast/src/views/infra/railjson.rs +++ b/editoast/src/views/infra/railjson.rs @@ -55,7 +55,7 @@ enum ListErrorsRailjson { )] async fn get_railjson( Path(infra): Path, - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result { let authorized = auth @@ -168,7 +168,11 @@ struct PostRailjsonResponse { ) )] async fn post_railjson( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Query(params): Query, Json(railjson): Json, @@ -181,8 +185,6 @@ async fn post_railjson( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); if railjson.version != RAILJSON_VERSION { return Err(ListErrorsRailjson::WrongRailjsonVersionProvided.into()); } diff --git a/editoast/src/views/infra/routes.rs b/editoast/src/views/infra/routes.rs index 6e52e1566b1..22c7b52ac2c 100644 --- a/editoast/src/views/infra/routes.rs +++ b/editoast/src/views/infra/routes.rs @@ -67,7 +67,7 @@ struct RoutesResponse { )] async fn get_routes_from_waypoint( Path(path): Path, - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result> { let authorized = auth @@ -145,7 +145,11 @@ struct RoutesFromNodesPositions { ), )] async fn get_routes_track_ranges( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(infra): Path, Query(params): Query, @@ -158,8 +162,8 @@ async fn get_routes_track_ranges( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); + let db_pool = db_pool.clone(); + let infra_caches = infra_caches.clone(); let infra_id = infra; let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, infra_id, || { InfraApiError::NotFound { infra_id } @@ -206,7 +210,11 @@ async fn get_routes_track_ranges( ), )] async fn get_routes_nodes( - app_state: State, + State(AppState { + db_pool, + infra_caches, + .. + }): State, Extension(auth): AuthenticationExt, Path(params): Path, Json(node_states): Json>>, @@ -219,9 +227,6 @@ async fn get_routes_nodes( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let infra_caches = app_state.infra_caches.clone(); - let infra = Infra::retrieve_or_fail(&mut db_pool.get().await?, params.infra_id, || { InfraApiError::NotFound { infra_id: params.infra_id, diff --git a/editoast/src/views/mod.rs b/editoast/src/views/mod.rs index ed34758915d..3b79ed76f56 100644 --- a/editoast/src/views/mod.rs +++ b/editoast/src/views/mod.rs @@ -334,8 +334,11 @@ async fn version() -> Json { (status = 200, description = "Return the core service version", body = Version), ), )] -async fn core_version(app_state: State) -> Json { - let core = app_state.core_client.clone(); +async fn core_version( + State(AppState { + core_client: core, .. + }): State, +) -> Json { let response = CoreVersionRequest {}.fetch(&core).await; let response = response.unwrap_or(Version { git_describe: None }); Json(response) diff --git a/editoast/src/views/timetable.rs b/editoast/src/views/timetable.rs index e8595eaf56a..a78b13f0846 100644 --- a/editoast/src/views/timetable.rs +++ b/editoast/src/views/timetable.rs @@ -120,7 +120,7 @@ struct TimetableIdParam { ), )] async fn get( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Path(timetable_id): Path, ) -> Result> { @@ -154,7 +154,7 @@ async fn get( ), )] async fn post( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, ) -> Result> { let authorized = auth @@ -183,7 +183,7 @@ async fn post( ), )] async fn delete( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, timetable_id: Path, ) -> Result { @@ -215,7 +215,7 @@ async fn delete( ) )] async fn train_schedule( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Path(timetable_id): Path, Json(train_schedules): Json>, @@ -271,7 +271,12 @@ pub struct ElectricalProfileSetIdQueryParam { ), )] async fn conflicts( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(timetable_id): Path, Query(infra_id_query): Query, @@ -285,10 +290,6 @@ async fn conflicts( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); - let timetable_id = timetable_id.id; let infra_id = infra_id_query.infra_id; let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; diff --git a/editoast/src/views/timetable/stdcm.rs b/editoast/src/views/timetable/stdcm.rs index 2b0a32de357..1abf7ef1ae3 100644 --- a/editoast/src/views/timetable/stdcm.rs +++ b/editoast/src/views/timetable/stdcm.rs @@ -120,7 +120,12 @@ struct InfraIdQueryParam { ) )] async fn stdcm( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Path(id): Path, Query(query): Query, @@ -134,11 +139,8 @@ async fn stdcm( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); let timetable_id = id; let infra_id = query.infra; diff --git a/editoast/src/views/train_schedule.rs b/editoast/src/views/train_schedule.rs index 2a2c8dabc5c..80fd72681bb 100644 --- a/editoast/src/views/train_schedule.rs +++ b/editoast/src/views/train_schedule.rs @@ -167,7 +167,7 @@ impl From for TrainScheduleChangeset { ) )] async fn get( - app_state: State, + State(db_pool): State, Extension(auth): AuthenticationExt, train_schedule_id: Path, ) -> Result> { @@ -179,7 +179,6 @@ async fn get( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let train_schedule_id = train_schedule_id.id; let conn = &mut db_pool.get().await?; @@ -205,7 +204,7 @@ struct BatchRequest { ) )] async fn get_batch( - app_state: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(BatchRequest { ids: train_ids }): Json, ) -> Result>> { @@ -217,7 +216,6 @@ async fn get_batch( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; let train_schedules: Vec = TrainSchedule::retrieve_batch_or_fail(conn, train_ids, |missing| { @@ -239,7 +237,7 @@ async fn get_batch( ) )] async fn delete( - app_state: State, + State(db_pool): State, Extension(auth): AuthenticationExt, Json(BatchRequest { ids: train_ids }): Json, ) -> Result { @@ -251,8 +249,6 @@ async fn delete( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - use crate::models::DeleteBatch; let conn = &mut db_pool.get().await?; TrainSchedule::delete_batch_or_fail(conn, train_ids, |number| { @@ -274,7 +270,7 @@ async fn delete( ) )] async fn put( - db_pool: State, + State(db_pool): State, Extension(auth): AuthenticationExt, train_schedule_id: Path, Json(train_schedule_form): Json, @@ -323,7 +319,12 @@ pub struct ElectricalProfileSetIdQueryParam { ), )] async fn simulation( - app_state: State, + State(AppState { + valkey: valkey_client, + core_client, + db_pool, + .. + }): State, Extension(auth): AuthenticationExt, Path(train_schedule_id): Path, Query(infra_id_query): Query, @@ -337,10 +338,6 @@ async fn simulation( return Err(AuthorizationError::Unauthorized.into()); } - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); - let db_pool = app_state.db_pool.clone(); - let infra_id = infra_id_query.infra_id; let electrical_profile_set_id = electrical_profile_set_id_query.electrical_profile_set_id; let train_schedule_id = train_schedule_id.id; @@ -657,7 +654,12 @@ enum SimulationSummaryResult { ), )] async fn simulation_summary( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client: core, + .. + }): State, Extension(auth): AuthenticationExt, Json(SimulationBatchForm { infra_id, @@ -673,10 +675,7 @@ async fn simulation_summary( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); let conn = &mut db_pool.get().await?; - let valkey_client = app_state.valkey.clone(); - let core = app_state.core_client.clone(); let infra = Infra::retrieve_or_fail(conn, infra_id, || TrainScheduleError::InfraNotFound { infra_id, @@ -759,7 +758,12 @@ async fn simulation_summary( ) )] async fn get_path( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client: core, + .. + }): State, Extension(auth): AuthenticationExt, Path(TrainScheduleIdParam { id: train_schedule_id, @@ -774,10 +778,6 @@ async fn get_path( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let core = app_state.core_client.clone(); - let conn = &mut db_pool.get().await?; let mut valkey_conn = valkey_client.get_connection().await?; diff --git a/editoast/src/views/train_schedule/projection.rs b/editoast/src/views/train_schedule/projection.rs index ff40379c62a..6c06a73c29a 100644 --- a/editoast/src/views/train_schedule/projection.rs +++ b/editoast/src/views/train_schedule/projection.rs @@ -126,7 +126,12 @@ struct CachedProjectPathTrainResult { ), )] async fn project_path( - app_state: State, + State(AppState { + db_pool, + valkey: valkey_client, + core_client, + .. + }): State, Extension(auth): AuthenticationExt, Json(ProjectPathForm { infra_id, @@ -150,10 +155,6 @@ async fn project_path( return Err(AuthorizationError::Unauthorized.into()); } - let db_pool = app_state.db_pool.clone(); - let valkey_client = app_state.valkey.clone(); - let core_client = app_state.core_client.clone(); - let ProjectPathInput { track_section_ranges: path_track_ranges, routes: path_routes,