-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
68 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod context; | |
pub mod datetime; | ||
pub mod env; | ||
pub mod folder; | ||
pub mod routes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |