Skip to content

Commit

Permalink
chore: Cleanup code by creating NestedRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
RemiBardon committed Dec 18, 2023
1 parent faf4ebc commit aaf5122
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 64 deletions.
50 changes: 50 additions & 0 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// prose-pod-api
//
// Copyright: 2023, Rémi Bardon <[email protected]>
// License: Mozilla Public License v2.0 (MPL v2.0)

use rocket::{Route, Rocket, Build};

type PrefixedRoutes = Vec<(String, Vec<Route>)>;

pub struct NestedRoutes {
inner: PrefixedRoutes,
}

impl NestedRoutes {
pub fn new() -> Self {
Self { inner: vec![] }
}
pub fn nesting(self, prefix: &'static str, other: NestedRoutes) -> Self {
let mut inner: PrefixedRoutes = self.inner;
inner.extend(other.prefixed(prefix).inner);
Self { inner }
}
pub fn prefixed(self, prefix: &'static str) -> Self {
let inner: PrefixedRoutes = self.inner
.into_iter()
.map(|(k, v)| (format!("{}/{}", prefix, k), v))
.collect();
Self { inner }
}
pub fn mount(self, rocket: Rocket<Build>) -> Rocket<Build> {
self.inner.into_iter()
.fold(rocket, |acc, (base, routes)| acc.mount(base, routes))
}
}

impl From<Vec<Route>> for NestedRoutes {
fn from(routes: Vec<Route>) -> Self {
Self { inner: vec![("".to_string(), routes)] }
}
}

impl From<Vec<(&'static str, NestedRoutes)>> for NestedRoutes {
fn from(vec: Vec<(&'static str, NestedRoutes)>) -> Self {
let mut inner: PrefixedRoutes = vec![];
for (p, k) in vec {
inner.extend(k.prefixed(p).inner);
}
Self { inner }
}
}
5 changes: 2 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

#[macro_use] extern crate rocket;

pub mod helpers;
pub mod v1;

#[launch]
fn rocket() -> _ {
let mut rocket = rocket::build();

for (base, routes) in v1::routes() {
rocket = rocket.mount(format!("/v1/{}", base), routes)
}
rocket = v1::routes().prefixed("/v1").mount(rocket);

rocket
}
6 changes: 3 additions & 3 deletions src/v1/members/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ mod routes;
pub use models::*;
pub use routes::*;

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<Route> {
pub fn routes() -> NestedRoutes {
routes![
get_members,
invite_member,
Expand All @@ -23,5 +23,5 @@ pub fn routes() -> Vec<Route> {
set_member_role,
set_member_mfa,
logout_member,
]
].into()
}
26 changes: 7 additions & 19 deletions src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,12 @@ mod workspace;

pub use routes::*;

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<(String, Vec<Route>)> {
fn prefix(prefix: &'static str, vec: Vec<Route>) -> Vec<(String, Vec<Route>)> {
vec![(prefix.to_string(), vec)]
}
fn nest(prefix: &'static str, vec: Vec<(String, Vec<Route>)>) -> Vec<(String, Vec<Route>)> {
vec
.into_iter()
.map(|(k, v)| (format!("{}/{}", prefix, k), v))
.collect()
}

vec![]
.into_iter()
.chain(prefix("", routes![openapi]))
.chain(prefix("members", members::routes()))
.chain(nest("server", server::routes()))
.chain(prefix("workspace", workspace::routes()))
.collect()
pub fn routes() -> NestedRoutes {
NestedRoutes::new()
.nesting("", routes![openapi].into())
.nesting("members", members::routes())
.nesting("server", server::routes())
.nesting("workspace", workspace::routes().into())
}
7 changes: 3 additions & 4 deletions src/v1/server/backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// Copyright: 2023, Rémi Bardon <[email protected]>
// License: Mozilla Public License v2.0 (MPL v2.0)

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<(String, Vec<Route>)> {
vec![
]
pub fn routes() -> NestedRoutes {
NestedRoutes::new()
}
6 changes: 3 additions & 3 deletions src/v1/server/features/compliance_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ mod routes;

pub use routes::*;

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<Route> {
pub fn routes() -> NestedRoutes {
routes![
get_compliance_tests_results,
get_compliance_suite_results,
set_compliance_test_suites,
]
].into()
}
6 changes: 3 additions & 3 deletions src/v1/server/features/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ mod routes;

pub use routes::*;

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<Route> {
pub fn routes() -> NestedRoutes {
routes![
get_features_config,
store_message_archive,
Expand All @@ -19,5 +19,5 @@ pub fn routes() -> Vec<Route> {
file_storage_encryption_scheme,
file_retention,
expunge_file_storage,
]
].into()
}
10 changes: 5 additions & 5 deletions src/v1/server/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
mod compliance_tests;
mod config;

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<(String, Vec<Route>)> {
pub fn routes() -> NestedRoutes {
vec![
("compliance-tests".to_string(), compliance_tests::routes()),
("config".to_string(), config::routes()),
]
("compliance-tests", compliance_tests::routes()),
("config", config::routes()),
].into()
}
23 changes: 7 additions & 16 deletions src/v1/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,12 @@ pub mod features;
pub mod network;
pub mod security;

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<(String, Vec<Route>)> {
fn nest(prefix: &'static str, vec: Vec<(String, Vec<Route>)>) -> Vec<(String, Vec<Route>)> {
vec
.into_iter()
.map(|(k, v)| (format!("{}/{}", prefix, k), v))
.collect()
}

vec![]
.into_iter()
.chain(nest("backup", backup::routes()))
.chain(nest("features", features::routes()))
.chain(nest("network", network::routes()))
.chain(nest("security", security::routes()))
.collect()
pub fn routes() -> NestedRoutes {
NestedRoutes::new()
.nesting("backup", backup::routes())
.nesting("features", features::routes())
.nesting("network", network::routes())
.nesting("security", security::routes())
}
7 changes: 3 additions & 4 deletions src/v1/server/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// Copyright: 2023, Rémi Bardon <[email protected]>
// License: Mozilla Public License v2.0 (MPL v2.0)

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<(String, Vec<Route>)> {
vec![
]
pub fn routes() -> NestedRoutes {
NestedRoutes::new()
}
7 changes: 3 additions & 4 deletions src/v1/server/security/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// Copyright: 2023, Rémi Bardon <[email protected]>
// License: Mozilla Public License v2.0 (MPL v2.0)

use rocket::Route;
use crate::helpers::NestedRoutes;

pub fn routes() -> Vec<(String, Vec<Route>)> {
vec![
]
pub fn routes() -> NestedRoutes {
NestedRoutes::new()
}

0 comments on commit aaf5122

Please sign in to comment.