From 7b52dcb1ef03bbe8680694b7afc4961e6fa95c1b Mon Sep 17 00:00:00 2001 From: Denux Date: Mon, 30 Dec 2024 22:05:56 +0100 Subject: [PATCH] temporary route fixes Fixes: https://github.com/loco-rs/loco/issues/1116 --- backend/src/app.rs | 17 +++------ backend/src/controllers/status.rs | 2 +- backend/src/utils/mod.rs | 1 + backend/src/utils/routes.rs | 61 +++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 backend/src/utils/routes.rs diff --git a/backend/src/app.rs b/backend/src/app.rs index a723a41b..eb287f23 100644 --- a/backend/src/app.rs +++ b/backend/src/app.rs @@ -1,6 +1,7 @@ use crate::initializers::openapi::OpenApiInitializer; use crate::initializers::services::ServicesInitializer; use crate::utils::folder::{create_necessary_folders, STORAGE_FOLDER}; +use crate::utils::routes::ExtendedAppRoutes; use crate::workers::session_used::SessionUsedWorker; use crate::{controllers, models::_entities::users, tasks}; use async_trait::async_trait; @@ -51,11 +52,10 @@ impl Hooks for App { } fn routes(_ctx: &AppContext) -> AppRoutes { - // TODO fix AppRoutes somehow. + // TODO fix AppRoutes somehow and remove custom ExtendedAppRoutes // Currently fucked. See issue: https://github.com/loco-rs/loco/issues/1116 - // The following should work: - /* - AppRoutes::with_default_routes() // controller routes below + + ExtendedAppRoutes::empty() .prefix("/api") .add_route(controllers::status::non_versioned_routes()) .prefix("/v1") @@ -63,14 +63,7 @@ impl Hooks for App { .add_route(controllers::session::routes()) .add_route(controllers::openapi::routes()) .add_route(controllers::status::routes()) - */ - - AppRoutes::with_default_routes() - .prefix("/api/v1") - .add_route(controllers::user::routes()) - .add_route(controllers::session::routes()) - .add_route(controllers::openapi::routes()) - .add_route(controllers::status::routes()) + .into() } async fn after_context(ctx: AppContext) -> Result { diff --git a/backend/src/controllers/status.rs b/backend/src/controllers/status.rs index d3c9fd28..b7d206d6 100644 --- a/backend/src/controllers/status.rs +++ b/backend/src/controllers/status.rs @@ -21,7 +21,7 @@ async fn health(Extension(status_service): Extension) -> (StatusC ) } -/// Version of the API. DOESN'T CURRENTLY WORK! +/// Version of the API. /// /// This endpoint is used to get the version of the API. /// This endpoint is not versioned so we try to keep it as simple as possible and not diff --git a/backend/src/utils/mod.rs b/backend/src/utils/mod.rs index 94155307..2aba2b0d 100644 --- a/backend/src/utils/mod.rs +++ b/backend/src/utils/mod.rs @@ -2,3 +2,4 @@ pub mod context; pub mod datetime; pub mod env; pub mod folder; +pub mod routes; diff --git a/backend/src/utils/routes.rs b/backend/src/utils/routes.rs new file mode 100644 index 00000000..a7c4ed30 --- /dev/null +++ b/backend/src/utils/routes.rs @@ -0,0 +1,61 @@ +use loco_rs::controller::AppRoutes; +use loco_rs::prelude::Routes; + +pub struct ExtendedAppRoutes { + prefix: Option, + internal_app_routes: AppRoutes, +} + +impl ExtendedAppRoutes { + pub fn empty() -> Self { + Self { + prefix: None, + internal_app_routes: AppRoutes::empty(), + } + } + + pub fn prefix(mut self, prefix: &str) -> Self { + match self.prefix { + None => self.prefix = Some(prefix.to_string()), + Some(mut old_prefix) => { + old_prefix.push_str(prefix); + self.prefix = Some(old_prefix.to_string()) + } + } + + self + } + + pub fn reset_prefix(mut self) -> Self { + self.prefix = None; + + self + } + + pub fn add_route(mut self, mut routes: Routes) -> Self { + let routes_prefix = { + if let Some(mut prefix) = self.prefix.clone() { + let routes_prefix = routes.prefix.clone().unwrap_or("".to_string()); + + prefix.push_str(routes_prefix.as_str()); + Some(prefix) + } else { + routes.prefix.clone() + } + }; + + if let Some(prefix) = routes_prefix { + routes = routes.prefix(prefix.as_str()); + } + + self.internal_app_routes = self.internal_app_routes.add_route(routes); + + self + } +} + +impl From for AppRoutes { + fn from(value: ExtendedAppRoutes) -> Self { + value.internal_app_routes + } +}