Skip to content

Commit

Permalink
temporary route fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DenuxPlays committed Dec 30, 2024
1 parent 9a437da commit 7b52dcb
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 deletions.
17 changes: 5 additions & 12 deletions backend/src/app.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -51,26 +52,18 @@ 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")
.add_route(controllers::user::routes())
.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<AppContext> {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/controllers/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn health(Extension(status_service): Extension<StatusService>) -> (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
Expand Down
1 change: 1 addition & 0 deletions backend/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod context;
pub mod datetime;
pub mod env;
pub mod folder;
pub mod routes;
61 changes: 61 additions & 0 deletions backend/src/utils/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use loco_rs::controller::AppRoutes;
use loco_rs::prelude::Routes;

pub struct ExtendedAppRoutes {
prefix: Option<String>,
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<ExtendedAppRoutes> for AppRoutes {
fn from(value: ExtendedAppRoutes) -> Self {
value.internal_app_routes
}
}

0 comments on commit 7b52dcb

Please sign in to comment.